zoukankan      html  css  js  c++  java
  • java用org.apache.poi包操作excel

    1.Jakarta POI 是apache的子项目,目标是处理ol2对象。它提供了一组Windows文档操作的Java API。

    2.EXCEL 结构
    HSSFWorkbook excell 文档对象介绍
    HSSFSheet excell的表单
    HSSFRow excell的行
    HSSFCell excell的格子单元
    HSSFFont excell字体
    HSSFName 名称
    HSSFDataFormat 日期格式
    在poi1.7中才有以下2项:
    HSSFHeader sheet头
    HSSFFooter sheet尾
    和这个样式
    HSSFCellStyle cell样式
    辅助操作包括
    HSSFDateUtil 日期
    HSSFPrintSetup 打印
    HSSFErrorConstants 错误信息表

    3.简单的用法

    创建Excel

    Java代码  收藏代码
    1. import java.io.FileOutputStream;  
    2.   
    3. import org.apache.poi.hssf.usermodel.HSSFWorkbook;  
    4.   
    5. public class NewWorkbook {  
    6.       
    7.     public static String outputFile = "C:/test1.xls";  
    8.     public static void main(String[] args) {  
    9.           
    10.         try {  
    11.             HSSFWorkbook wb = new HSSFWorkbook();//create new HSSFWorkbook object  
    12.             FileOutputStream fileOut = new FileOutputStream(outputFile);  
    13.             wb.write(fileOut);//Workbook-->test1.xls  
    14.             fileOut.flush();  
    15.             fileOut.close();  
    16.             System.out.println("The file has been created.");  
    17.         } catch (Exception e) {  
    18.             e.printStackTrace();  
    19.         }  
    20.     }  
    21.   
    22. }  

    简单的Excel写操作

    Java代码  收藏代码
    1. import java.io.FileOutputStream;  
    2. import java.io.IOException;  
    3. import java.util.Date;  
    4.   
    5. import org.apache.poi.hssf.usermodel.HSSFCell;  
    6. import org.apache.poi.hssf.usermodel.HSSFCellStyle;  
    7. import org.apache.poi.hssf.usermodel.HSSFDataFormat;  
    8. import org.apache.poi.hssf.usermodel.HSSFRow;  
    9. import org.apache.poi.hssf.usermodel.HSSFSheet;  
    10. import org.apache.poi.hssf.usermodel.HSSFWorkbook;  
    11.   
    12. public class CreateCells {  
    13.   
    14.     public static String fileTobewrite = "C:/test1.xls";  
    15.     public static void main(String[] args) throws IOException {  
    16.         try {  
    17.             HSSFWorkbook wb = new HSSFWorkbook();//create new HSSFWorkbook object  
    18.             HSSFSheet sheet = wb.createSheet("new sheet");// create new sheet object  
    19.             //Create a row and put some cells in it. Rows are 0.  
    20.             HSSFRow row = sheet.createRow(0);//create new row  
    21.             //Create a cell and put a value in it.  
    22.             HSSFCell cell = row.createCell(0);//create new cell  
    23.             cell.setCellValue(1);//setting the cell value  
    24.               
    25.             //do it on one line  
    26.             row.createCell(1).setCellValue(1.2);  
    27.             row.createCell(2).setCellValue("test");  
    28.             row.createCell(3).setCellValue(true);  
    29.             HSSFCellStyle cellStyle = wb.createCellStyle();//new cell style  
    30.             cellStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat("m/d/yy h:mm"));// set date style  
    31.             HSSFCell dcell = row.createCell(4);//create new cell  
    32.             dcell.setCellValue(new Date());  
    33.             dcell.setCellStyle(cellStyle);  
    34.             HSSFCell csCell = row.createCell(5);  
    35.             csCell.setCellType(HSSFCell.ENCODING_UTF_16);  
    36.             csCell.setCellValue("中文测试_Chinese Words Test");//set cell code   
    37.               
    38.             row.createCell(6).setCellType(HSSFCell.CELL_TYPE_ERROR);  
    39.               
    40.             //write the output to a file  
    41.             FileOutputStream fileOut = new FileOutputStream(fileTobewrite);  
    42.             wb.write(fileOut);  
    43.             fileOut.flush();  
    44.             fileOut.close();  
    45.             System.out.println("The cells have been added.");  
    46.         } catch (Exception e) {  
    47.             e.printStackTrace();  
    48.         }  
    49.     }  
    50.   
    51.   
    52. }  

    简单的Excel读操作

    Java代码  收藏代码
    1. import java.io.FileInputStream;  
    2. import java.text.DateFormat;  
    3. import java.text.SimpleDateFormat;  
    4.   
    5. import org.apache.poi.hssf.usermodel.HSSFCell;  
    6. import org.apache.poi.hssf.usermodel.HSSFDateUtil;  
    7. import org.apache.poi.hssf.usermodel.HSSFRow;  
    8. import org.apache.poi.hssf.usermodel.HSSFSheet;  
    9. import org.apache.poi.hssf.usermodel.HSSFWorkbook;  
    10.   
    11. public class ReadExcel {  
    12.     public static String fileTobeRead = "C:/test1.xls";  
    13.     public static String getCellValue(HSSFCell cell){  
    14.         String value = null;  
    15.         if (cell != null)  
    16.         {  
    17.             //get the type of the cell  
    18.             int cellType = cell.getCellType();  
    19.             switch (cellType)  
    20.             {  
    21.             //""  
    22.             case HSSFCell.CELL_TYPE_BLANK :  
    23.                 value = "";  
    24.                 break;  
    25.             //Boolean  
    26.             case HSSFCell.CELL_TYPE_BOOLEAN :  
    27.                 value = cell.getBooleanCellValue() ? "TRUE" : "FALSE";  
    28.                 break;  
    29.             //Error  
    30.             case HSSFCell.CELL_TYPE_ERROR :  
    31.                 value = "ERR-" + cell.getErrorCellValue();  
    32.                 break;  
    33.             //Formula  
    34.             case HSSFCell.CELL_TYPE_FORMULA :  
    35.                 value = cell.getCellFormula();  
    36.                 break;  
    37.             //Numeric  
    38.             case HSSFCell.CELL_TYPE_NUMERIC :  
    39.                 //Date  
    40.                 if (HSSFDateUtil.isCellDateFormatted(cell))  
    41.                 {  
    42.                     //change to "yyyy-MM-dd"  
    43.                     DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");  
    44.                     value = sdf.format(cell.getDateCellValue());  
    45.                 }     
    46.                 //Number  
    47.                 else  
    48.                 {  
    49.                     value = cell.getNumericCellValue() + "";  
    50.                 }  
    51.                 break;  
    52.             //String  
    53.             case HSSFCell.CELL_TYPE_STRING :  
    54.                 value = cell.getStringCellValue();  
    55.                 break;  
    56.             //Other  
    57.             default :  
    58.                 value = "Unknown Cell Type:"  + cell.getCellType();  
    59.             }  
    60.         }  
    61.         return value;  
    62.           
    63.     }  
    64.       
    65.     public static void main(String[] args) {  
    66.         try {  
    67.             HSSFWorkbook wb = new HSSFWorkbook(new FileInputStream(fileTobeRead));  
    68.               
    69.             HSSFSheet sheet = wb.getSheet("new sheet");  
    70.             //getSheetAt(int index) first sheet index is 0.  
    71.             int rowNum = sheet.getPhysicalNumberOfRows();  
    72.             int cellNum;  
    73.             System.out.println("Row number is " + rowNum);  
    74.             HSSFRow row;  
    75.             HSSFCell cell;  
    76.             for(int i=0;i<sheet.getPhysicalNumberOfRows();i++){  
    77.                   
    78.                 row = sheet.getRow(i);  
    79.                 cellNum = row.getPhysicalNumberOfCells();  
    80.                 System.out.println("cell number is " + cellNum);  
    81.                 for(int j = 0; j < cellNum; j++){  
    82.                     cell=row.getCell(j);  
    83.                       
    84.                     System.out.println("row " + i + "cell "+ j + ":" + getCellValue(cell));  
    85.                 }  
    86.             }  
    87.               
    88.         } catch (Exception e) {  
    89.             e.printStackTrace();  
    90.         }  
    91.     }  
    92.   
    93. }  

    4.设置单元格格式

    Java代码  收藏代码
      1. //set font style red and bold  
      2. HSSFFont font = wb.createFont();  
      3. font.setColor(HSSFFont.COLOR_RED);  
      4. font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);  
      5.               
      6. //create style  
      7. HSSFCellStyle cellStyle1 = wb.createCellStyle();  
      8. cellStyle1.setFont(font);  
      9.               
      10. //use this style  
      11. HSSFCell cell1 = row.createCell(1);  
      12. cell.setCellStyle(cellStyle1);  
      13. cell.setCellType(HSSFCell.CELL_TYPE_STRING);  
      14. cell.setCellValue("Title");  
  • 相关阅读:
    SDUT 2143 图结构练习——最短路径 SPFA模板,方便以后用。。 Anti
    SDUT ACM 1002 Biorhythms 中国剩余定理 Anti
    nyist OJ 119 士兵杀敌(三) RMQ问题 Anti
    SDUT ACM 2157 Greatest Number Anti
    SDUT ACM 2622 最短路径 二维SPFA启蒙题。。 Anti
    二叉索引树 区间信息的维护与查询 Anti
    SDUT ACM 2600 子节点计数 Anti
    UVA 1428 Ping pong 二叉索引树标准用法 Anti
    2010圣诞Google首页效果
    Object
  • 原文地址:https://www.cnblogs.com/gtaxmjld/p/4185711.html
Copyright © 2011-2022 走看看