• Java操作Excel和Word


    这是一个URL它提供了Java项目所推荐的处理此项目所用的类库

    http://www.oschina.net/project/tag/258/excel-tools?company=0&sort=score&tag=258&lang=19&recommend=false

    Java用Apache的POI库对Excel进行操作;

    其他Excel开发库: http://www.oschina.net/project/tag/258/excel-tools

    Apache POI 下载地址: http://poi.apache.org/download.html

    转载链接:http://www.cnblogs.com/wunaozai/p/4179270.html

    Excel 2003 (xls后缀)

      读:

     1 import java.io.FileInputStream;
     2 
     3 import org.apache.poi.hssf.usermodel.HSSFRow;
     4 import org.apache.poi.hssf.usermodel.HSSFSheet;
     5 import org.apache.poi.hssf.usermodel.HSSFWorkbook;
     6 
     7 public class ReadExcel2003 {
     8     public static void main(String[] args) {
     9         try {
    10             FileInputStream file = new FileInputStream("C:\dd.xls");
    11             
    12             HSSFWorkbook workbook = new HSSFWorkbook(file);
    13             HSSFSheet sheet = workbook.getSheetAt(0);
    14             HSSFRow row = null;
    15 
    16             for(int i=0;sheet.getRow(i)!=null;i++){
    17                 row = sheet.getRow(i);
    18                 for(int j=0;row.getCell(j)!=null;j++){
    19                     System.out.print(row.getCell(j)+"");
    20                 }
    21                 System.out.println();
    22             }
    23         } catch (Exception e) {
    24             e.printStackTrace();
    25         }
    26     }
    27 }

      

     1 import java.io.FileOutputStream;
     2 
     3 import org.apache.poi.hssf.usermodel.HSSFCell;
     4 import org.apache.poi.hssf.usermodel.HSSFRow;
     5 import org.apache.poi.hssf.usermodel.HSSFSheet;
     6 import org.apache.poi.hssf.usermodel.HSSFWorkbook;
     7 
     8 public class WriteExcel2003 {
     9     public static void main(String[] args) {
    10         try {
    11             HSSFWorkbook workbook = new HSSFWorkbook();
    12             HSSFSheet sheet = workbook.createSheet("test"); //页签
    13             HSSFRow row = sheet.createRow(1);
    14             HSSFCell cell = row.createCell(2);
    15             cell.setCellValue("test");
    16             for (int i=0;i<100;i++){
    17                 row = sheet.createRow(i);
    18                 for(int j=0;j<100;j++){
    19                     cell = row.createCell(j);
    20                     cell.setCellValue("("+i+","+j+")");
    21                 }
    22             }
    23             FileOutputStream os = null;
    24             os = new FileOutputStream("C:\dd.xls");
    25             workbook.write(os);
    26             os.flush();
    27             os.close();
    28         } catch (Exception e) {
    29             e.printStackTrace();
    30         }
    31         System.out.println("Ok");
    32     }
    33 }

    Excel 2007 (xlsx 后缀)

      读:

     1 import java.io.FileInputStream;
     2 
     3 import org.apache.poi.xssf.usermodel.XSSFRow;
     4 import org.apache.poi.xssf.usermodel.XSSFSheet;
     5 import org.apache.poi.xssf.usermodel.XSSFWorkbook;
     6 
     7 public class ReadExcel2007 {
     8     public static void main(String[] args) {
     9         try {
    10             FileInputStream file = new FileInputStream("C:\bb.xlsx");
    11             XSSFWorkbook workbook  = new XSSFWorkbook(file);
    12             XSSFSheet sheet = workbook.getSheetAt(0);
    13             
    14             XSSFRow row = null;
    15             
    16             for(int i=0;sheet.getRow(i)!=null;i++){
    17                 row = sheet.getRow(i);
    18                 for(int j=0;row.getCell(j)!=null;j++){
    19                     System.out.print(row.getCell(j)+" ");
    20                 }
    21                 System.out.println();
    22             }
    23         } catch (Exception e) {
    24             e.printStackTrace();
    25         }
    26     }
    27 }

      写:

     1 import java.io.FileOutputStream;
     2 
     3 import org.apache.poi.xssf.usermodel.XSSFCell;
     4 import org.apache.poi.xssf.usermodel.XSSFRow;
     5 import org.apache.poi.xssf.usermodel.XSSFSheet;
     6 import org.apache.poi.xssf.usermodel.XSSFWorkbook;
     7 
     8 public class WriteExcel2007 {
     9     public static void main(String[] args) {
    10         try {
    11             XSSFWorkbook workbook = new XSSFWorkbook();
    12             XSSFSheet sheet = workbook.createSheet("test"); //页签
    13             XSSFRow row = sheet.createRow(1);
    14             XSSFCell cell = row.createCell(2);
    15             cell.setCellValue("test");
    16             for (int i=0;i<100;i++){
    17                 row = sheet.createRow(i);
    18                 for(int j=0;j<100;j++){
    19                     cell = row.createCell(j);
    20                     cell.setCellValue("("+i+","+j+")");
    21                 }
    22             }
    23             FileOutputStream os = null;
    24             os = new FileOutputStream("C:\dd.xlsx");
    25             workbook.write(os);
    26             os.flush();
    27             os.close();
    28         } catch (Exception e) {
    29             e.printStackTrace();
    30         }
    31         System.out.println("Ok");
    32     }
    33 }
  • 相关阅读:
    【重点突破】——Canvas技术绘制音乐播放器界面
    【重点突破】——Canvas技术绘制随机改变的验证码
    【重点突破】——使用Canvas进行绘图图像
    【温故知新】——HTML5重要知识点复习
    【重点突破】——Cookie的使用
    vs2012 MinGW 编译ffmeg 引用外部库libx264,librtmp
    vs2015编译ffmpeg 出现错误rtmp.lib(rtmp.obj) : error LNK2001: 无法解析的外部符号 ___iob_func
    vs2012 MinGW编译ffmpeg 出现libavdevice/avdevice.c(38) : error C2059: 语法错误:“.”
    ffmpeg rtmp 推流错误WriteN, RTMP send error 10053 10038
    ffmpeg 发布hls流
  • 原文地址:https://www.cnblogs.com/lubolin/p/6959588.html
Copyright © 2020-2023  润新知