操作Excel工具类之基于Apache的POI类库

来源:互联网 发布:linux挂载samba共享 编辑:程序博客网 时间:2024/06/05 03:15

功能简介:

1、向Excel文档插入数据,可以是多行可以是多列,保留原单元格格式不变

2、向Excel文档插入一个新行,并且使用与上一行完全相同的格式

3、等等

 

需要的第三方JAR包:

poi-3.8-20120326.jar

poi-examples-3.8-20120326.jar

poi-excelant-3.8-20120326.jar

poi-ooxml-3.8-20120326.jar

poi-ooxml-schemas-3.8-20120326.jar

poi-scratchpad-3.8-20120326.jar

stax-api-1.0.1.jar

 

完整的工具类的代码如下:

感谢gegewuqin9和在世界的中心呼喚愛在回复中建议,对读取单元格值的地方做了修改。

 

 

Java代码  收藏代码
  1. import java.io.File;  
  2. import java.io.FileInputStream;  
  3. import java.io.FileOutputStream;  
  4. import java.io.InputStream;  
  5.   
  6. import org.apache.poi.ss.usermodel.Cell;  
  7. import org.apache.poi.ss.usermodel.CellStyle;  
  8. import org.apache.poi.ss.usermodel.Row;  
  9. import org.apache.poi.ss.usermodel.Sheet;  
  10. import org.apache.poi.ss.usermodel.Workbook;  
  11. import org.apache.poi.ss.usermodel.WorkbookFactory;  
  12.   
  13. /** 
  14.  * Excel工具类 
  15.  *  
  16.  * <pre> 
  17.  * 基于Apache的POI类库 
  18.  * </pre> 
  19.  *  
  20.  * @author 陈峰 
  21.  */  
  22. public class POIExcelMakerUtil {  
  23.   
  24.     private File excelFile;  
  25.   
  26.     private InputStream fileInStream;  
  27.   
  28.     private Workbook workBook;  
  29.   
  30.     public POIExcelMakerUtil(File file) throws Exception {  
  31.         this.excelFile = file;  
  32.         this.fileInStream = new FileInputStream(this.excelFile);  
  33.         this.workBook = WorkbookFactory.create(this.fileInStream);  
  34.     }  
  35.   
  36.     /** 
  37.      * 写入一组值 
  38.      *  
  39.      * @param sheetNum 
  40.      *            写入的sheet的编号 
  41.      * @param fillRow 
  42.      *            是写入行还是写入列 
  43.      * @param startRowNum 
  44.      *            开始行号 
  45.      * @param startColumnNum 
  46.      *            开始列号 
  47.      * @param contents 
  48.      *            写入的内容数组 
  49.      * @throws Exception 
  50.      */  
  51.     public void writeArrayToExcel(int sheetNum, boolean fillRow,  
  52.             int startRowNum, int startColumnNum, Object[] contents)  
  53.             throws Exception {  
  54.         Sheet sheet = this.workBook.getSheetAt(sheetNum);  
  55.         writeArrayToExcel(sheet, fillRow, startRowNum, startColumnNum, contents);  
  56.     }  
  57.   
  58.     /** 
  59.      * 写入一组值 
  60.      *  
  61.      * @param sheetNum 
  62.      *            写入的sheet的名称 
  63.      * @param fillRow 
  64.      *            是写入行还是写入列 
  65.      * @param startRowNum 
  66.      *            开始行号 
  67.      * @param startColumnNum 
  68.      *            开始列号 
  69.      * @param contents 
  70.      *            写入的内容数组 
  71.      * @throws Exception 
  72.      */  
  73.     public void writeArrayToExcel(String sheetName, boolean fillRow,  
  74.             int startRowNum, int startColumnNum, Object[] contents)  
  75.             throws Exception {  
  76.         Sheet sheet = this.workBook.getSheet(sheetName);  
  77.         writeArrayToExcel(sheet, fillRow, startRowNum, startColumnNum, contents);  
  78.     }  
  79.   
  80.     private void writeArrayToExcel(Sheet sheet, boolean fillRow,  
  81.             int startRowNum, int startColumnNum, Object[] contents)  
  82.             throws Exception {  
  83.         for (int i = 0, length = contents.length; i < length; i++) {  
  84.             int rowNum;  
  85.             int columnNum;  
  86.             // 以行为单位写入  
  87.             if (fillRow) {  
  88.                 rowNum = startRowNum;  
  89.                 columnNum = startColumnNum + i;  
  90.             }  
  91.             //  以列为单位写入  
  92.             else {  
  93.                 rowNum = startRowNum + i;  
  94.                 columnNum = startColumnNum;  
  95.             }  
  96.             this.writeToCell(sheet, rowNum, columnNum,  
  97.                     convertString(contents[i]));  
  98.         }  
  99.     }  
  100.   
  101.     /** 
  102.      * 向一个单元格写入值 
  103.      *  
  104.      * @param sheetNum 
  105.      *            sheet的编号 
  106.      * @param rowNum 
  107.      *            行号 
  108.      * @param columnNum 
  109.      *            列号 
  110.      * @param value 
  111.      *            写入的值 
  112.      * @throws Exception 
  113.      */  
  114.     public void writeToExcel(int sheetNum, int rowNum, int columnNum,  
  115.             Object value) throws Exception {  
  116.         Sheet sheet = this.workBook.getSheetAt(sheetNum);  
  117.         this.writeToCell(sheet, rowNum, columnNum, value);  
  118.     }  
  119.   
  120.     /** 
  121.      * 向一个单元格写入值 
  122.      *  
  123.      * @param sheetName 
  124.      *            sheet的名称 
  125.      * @param columnRowNum 
  126.      *            单元格的位置 
  127.      * @param value 
  128.      *            写入的值 
  129.      * @throws Exception 
  130.      */  
  131.     public void writeToExcel(String sheetName, int rowNum, int columnNum,  
  132.             Object value) throws Exception {  
  133.         Sheet sheet = this.workBook.getSheet(sheetName);  
  134.         this.writeToCell(sheet, rowNum, columnNum, value);  
  135.     }  
  136.   
  137.     /** 
  138.      * 向一个单元格写入值 
  139.      *  
  140.      * @param sheetNum 
  141.      *            sheet的编号 
  142.      * @param columnRowNum 
  143.      *            单元格的位置 
  144.      * @param value 
  145.      *            写入的值 
  146.      * @throws Exception 
  147.      */  
  148.     public void writeToExcel(int sheetNum, String columnRowNum, Object value)  
  149.             throws Exception {  
  150.         Sheet sheet = this.workBook.getSheetAt(sheetNum);  
  151.         this.writeToCell(sheet, columnRowNum, value);  
  152.     }  
  153.   
  154.     /** 
  155.      * 向一个单元格写入值 
  156.      *  
  157.      * @param sheetNum 
  158.      *            sheet的名称 
  159.      * @param columnRowNum 
  160.      *            单元格的位置 
  161.      * @param value 
  162.      *            写入的值 
  163.      * @throws Exception 
  164.      */  
  165.     public void writeToExcel(String sheetName, String columnRowNum, Object value)  
  166.             throws Exception {  
  167.         Sheet sheet = this.workBook.getSheet(sheetName);  
  168.         this.writeToCell(sheet, columnRowNum, value);  
  169.     }  
  170.   
  171.     private void writeToCell(Sheet sheet, String columnRowNum, Object value)  
  172.             throws Exception {  
  173.         int[] rowNumColumnNum = convertToRowNumColumnNum(columnRowNum);  
  174.         int rowNum = rowNumColumnNum[0];  
  175.         int columnNum = rowNumColumnNum[1];  
  176.         this.writeToCell(sheet, rowNum, columnNum, value);  
  177.     }  
  178.   
  179.     /** 
  180.      * 将单元格的行列位置转换为行号和列号 
  181.      *  
  182.      * @param columnRowNum 
  183.      *            行列位置 
  184.      * @return 长度为2的数组,第1位为行号,第2位为列号 
  185.      */  
  186.     private static int[] convertToRowNumColumnNum(String columnRowNum) {  
  187.         columnRowNum = columnRowNum.toUpperCase();  
  188.         char[] chars = columnRowNum.toCharArray();  
  189.         int rowNum = 0;  
  190.         int columnNum = 0;  
  191.         for (char c : chars) {  
  192.             if ((c >= 'A' && c <= 'Z')) {  
  193.                 columnNum = columnNum * 26 + ((int) c - 64);  
  194.             } else {  
  195.                 rowNum = rowNum * 10 + new Integer(c + "");  
  196.             }  
  197.         }  
  198.         return new int[] { rowNum - 1, columnNum - 1 };  
  199.     }  
  200.   
  201.     private void writeToCell(Sheet sheet, int rowNum, int columnNum,  
  202.             Object value) throws Exception {  
  203.         Row row = sheet.getRow(rowNum);  
  204.         Cell cell = row.getCell(columnNum);  
  205.         if (cell == null) {  
  206.             cell = row.createCell(columnNum);  
  207.         }  
  208.         cell.setCellValue(convertString(value));  
  209.     }  
  210.   
  211.     /** 
  212.      * 读取一个单元格的值 
  213.      *  
  214.      * @param sheetName 
  215.      *            sheet的名称 
  216.      * @param columnRowNum 
  217.      *            单元格的位置 
  218.      * @return 
  219.      * @throws Exception 
  220.      */  
  221.     public Object readCellValue(String sheetName, String columnRowNum)  
  222.             throws Exception {  
  223.         Sheet sheet = this.workBook.getSheet(sheetName);  
  224.         int[] rowNumColumnNum = convertToRowNumColumnNum(columnRowNum);  
  225.         int rowNum = rowNumColumnNum[0];  
  226.         int columnNum = rowNumColumnNum[1];  
  227.         Row row = sheet.getRow(rowNum);  
  228.         if (row != null) {  
  229.             Cell cell = row.getCell(columnNum);  
  230.             if (cell != null) {  
  231.                 return getCellValue(cell);  
  232.             }  
  233.         }  
  234.         return null;  
  235.     }  
  236.   
  237.     /** 
  238.      * 获取单元格中的值 
  239.      *  
  240.      * @param cell 单元格 
  241.      * @return 
  242.      */  
  243.     private static Object getCellValue(Cell cell) {  
  244.         int type = cell.getCellType();  
  245.         switch (type) {  
  246.         case Cell.CELL_TYPE_STRING:  
  247.             return (Object) cell.getStringCellValue();  
  248.         case Cell.CELL_TYPE_NUMERIC:  
  249.             Double value = cell.getNumericCellValue();  
  250.             return (Object) (value.intValue());  
  251.         case Cell.CELL_TYPE_BOOLEAN:  
  252.             return (Object) cell.getBooleanCellValue();  
  253.         case Cell.CELL_TYPE_FORMULA:  
  254.             return (Object) cell.getArrayFormulaRange().formatAsString();  
  255.         case Cell.CELL_TYPE_BLANK:  
  256.             return (Object) "";  
  257.         default:  
  258.             return null;  
  259.         }  
  260.     }  
  261.   
  262.     /** 
  263.      * 插入一行并参照与上一行相同的格式 
  264.      *  
  265.      * @param sheetNum 
  266.      *            sheet的编号 
  267.      * @param rowNum 
  268.      *            插入行的位置 
  269.      * @throws Exception 
  270.      */  
  271.     public void insertRowWithFormat(int sheetNum, int rowNum) throws Exception {  
  272.         Sheet sheet = this.workBook.getSheetAt(sheetNum);  
  273.         insertRowWithFormat(sheet, rowNum);  
  274.     }  
  275.   
  276.     /** 
  277.      * 插入一行并参照与上一行相同的格式 
  278.      *  
  279.      * @param sheetName 
  280.      *            sheet的名称 
  281.      * @param rowNum 
  282.      *            插入行的位置 
  283.      * @throws Exception 
  284.      */  
  285.     public void insertRowWithFormat(String sheetName, int rowNum)  
  286.             throws Exception {  
  287.         Sheet sheet = this.workBook.getSheet(sheetName);  
  288.         insertRowWithFormat(sheet, rowNum);  
  289.     }  
  290.   
  291.     private void insertRowWithFormat(Sheet sheet, int rowNum) throws Exception {  
  292.         sheet.shiftRows(rowNum, rowNum + 11);  
  293.         Row newRow = sheet.createRow(rowNum);  
  294.         Row oldRow = sheet.getRow(rowNum - 1);  
  295.         for (int i = oldRow.getFirstCellNum(); i < oldRow.getLastCellNum(); i++) {  
  296.             Cell oldCell = oldRow.getCell(i);  
  297.             if (oldCell != null) {  
  298.                 CellStyle cellStyle = oldCell.getCellStyle();  
  299.                 newRow.createCell(i).setCellStyle(cellStyle);  
  300.             }  
  301.         }  
  302.     }  
  303.   
  304.     /** 
  305.      * 重命名一个sheet 
  306.      *  
  307.      * @param sheetNum 
  308.      *            sheet的编号 
  309.      * @param newName 
  310.      *            新的名称 
  311.      */  
  312.     public void renameSheet(int sheetNum, String newName) {  
  313.         this.workBook.setSheetName(sheetNum, newName);  
  314.     }  
  315.   
  316.     /** 
  317.      * 重命名一个sheet 
  318.      *  
  319.      * @param oldName 
  320.      *            旧的名称 
  321.      * @param newName 
  322.      *            新的名称 
  323.      */  
  324.     public void renameSheet(String oldName, String newName) {  
  325.         int sheetNum = this.workBook.getSheetIndex(oldName);  
  326.         this.renameSheet(sheetNum, newName);  
  327.     }  
  328.   
  329.     /** 
  330.      * 删除一个sheet 
  331.      *  
  332.      * @param sheetName 
  333.      *            sheet的名称 
  334.      */  
  335.     public void removeSheet(String sheetName) {  
  336.         this.workBook.removeSheetAt(this.workBook.getSheetIndex(sheetName));  
  337.     }  
  338.   
  339.     /** 
  340.      * 写入Excel文件并关闭 
  341.      */  
  342.     public void writeAndClose() {  
  343.         if (this.workBook != null) {  
  344.             try {  
  345.                 FileOutputStream fileOutStream = new FileOutputStream(  
  346.                         this.excelFile);  
  347.                 this.workBook.write(fileOutStream);  
  348.                 if (fileOutStream != null) {  
  349.                     fileOutStream.close();  
  350.                 }  
  351.             } catch (Exception e) {  
  352.                 e.printStackTrace();  
  353.             }  
  354.         }  
  355.         if (this.fileInStream != null) {  
  356.             try {  
  357.                 this.fileInStream.close();  
  358.             } catch (Exception e) {  
  359.             }  
  360.         }  
  361.     }  
  362.   
  363.     private static String convertString(Object value) {  
  364.         if (value == null) {  
  365.             return "";  
  366.         } else {  
  367.             return value.toString();  
  368.         }  
  369.     }  
  370.   
  371. }  
原创粉丝点击