Excel文件相关操作

解决方案,使用Poi工具实现问题转换。

待解决问题:当xls文件损坏或者其他因素可能导致文件无法正常的读取。经过网上查找的各种解决方案均是无效。

  1 package com.excel.util;
  2 
  3 
  4 
  5 import org.apache.poi.hssf.usermodel.*;
  6 import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
  7 import org.apache.poi.poifs.filesystem.POIFSFileSystem;
  8 import org.apache.poi.ss.usermodel.*;
  9 import org.apache.poi.xssf.usermodel.XSSFWorkbook;
 10 
 11 
 12 import java.io.*;
 13 import java.text.SimpleDateFormat;
 14 import java.util.Iterator;
 15 
 16 public class Tests {
 17     /**时间格式*/
 18     private static final String OUTPUT_DATE_FORMAT= "yyyy/MM/dd";
 19     /**Cvs分隔符*/
 20     private static final String CVS_SEPERATOR_CHAR=",";
 21     /**新行*/
 22     private static final String NEW_LINE_CHARACTER="\r\n";
 23 
 24     /**
 25      * 转Xlsx
 26      * @param source
 27      * @param dest
 28      * @throws InvalidFormatException
 29      * @throws IOException
 30      */
 31     public static void transXlsx (String source,String dest) throws InvalidFormatException,
 32             IOException {
 33 //        String inpFnc = source;;
 34 //        String csv = source.replace(".xls",".csv");
 35         String inpFn = source; //transCsv(source,csv);
 36         String outFn = dest;
 37         InputStream in = new BufferedInputStream(new FileInputStream(inpFn));
 38         try {
 39             Workbook wbIn = new HSSFWorkbook(in);
 40             File outF = new File(outFn);
 41             if (outF.exists()) {
 42                 outF.delete();
 43             }
 44 
 45             Workbook wbOut = new XSSFWorkbook();
 46             int sheetCnt = wbIn.getNumberOfSheets();
 47             for (int i = 0; i < sheetCnt; i++) {
 48                 Sheet sIn = wbIn.getSheetAt(0);
 49                 Sheet sOut = wbOut.createSheet(sIn.getSheetName());
 50                 Iterator<Row> rowIt = sIn.rowIterator();
 51                 while (rowIt.hasNext()) {
 52                     Row rowIn = rowIt.next();
 53                     Row rowOut = sOut.createRow(rowIn.getRowNum());
 54 
 55                     Iterator<Cell> cellIt = rowIn.cellIterator();
 56                     while (cellIt.hasNext()) {
 57                         Cell cellIn = cellIt.next();
 58                         Cell cellOut = rowOut.createCell(
 59                                 cellIn.getColumnIndex(), cellIn.getCellType());
 60 
 61                         if (cellIn.getCellType() == CellType.BLANK ) {
 62                         }
 63 
 64 
 65                         else if (cellIn.getCellType() == CellType.BOOLEAN ) {
 66                             cellOut.setCellValue(cellIn.getBooleanCellValue());
 67                         }
 68 
 69                         else if (cellIn.getCellType() == CellType.ERROR ) {
 70                             cellOut.setCellValue(cellIn.getErrorCellValue());
 71                         }
 72 
 73                         else if (cellIn.getCellType() == CellType.FORMULA) {
 74                             cellOut.setCellFormula(cellIn.getCellFormula());
 75                         }
 76 
 77                         if (cellIn.getCellType() == CellType.NUMERIC) {
 78                             if (DateUtil.isCellDateFormatted(cellIn)){
 79                                 cellOut.setCellValue(new SimpleDateFormat(OUTPUT_DATE_FORMAT).format(cellIn.getDateCellValue()));
 80                             }
 81                             cellOut.setCellValue(cellIn.getNumericCellValue());
 82                         }
 83 
 84                         if (cellIn.getCellType() == CellType.STRING){
 85                                 cellOut.setCellValue(cellIn.getStringCellValue());
 86                         }
 87 
 88                         {
 89                             CellStyle styleIn = cellIn.getCellStyle();
 90                             CellStyle styleOut = cellOut.getCellStyle();
 91                             styleOut.setDataFormat(styleIn.getDataFormat());
 92                         }
 93                         cellOut.setCellComment(cellIn.getCellComment());
 94 
 95                         // HSSFCellStyle cannot be cast to XSSFCellStyle
 96                         // cellOut.setCellStyle(cellIn.getCellStyle());
 97                     }
 98                 }
 99             }
100             OutputStream out = new BufferedOutputStream(new FileOutputStream(outF));
101             try {
102                 wbOut.write(out);
103             } finally {
104                 out.close();
105             }
106         } finally {
107             in.close();
108         }
109     }
110 
111     /**
112      * 转Csv
113      * @param fileName
114      * @param csvFileName
115      * @return
116      * @throws IOException
117      */
118     private static String transCsv(String fileName,String csvFileName) throws IOException {
119     String excelFileName = fileName;
120     HSSFWorkbook myWorkBook = new HSSFWorkbook(new POIFSFileSystem(new FileInputStream(excelFileName)));
121     HSSFSheet mySheet = myWorkBook.getSheetAt(0);
122     Iterator rowIter = mySheet.rowIterator();
123     String csvData = "";
124     while (rowIter.hasNext()) {
125         HSSFRow myRow = (HSSFRow) rowIter.next();
126         for (int i = 0; i < myRow.getLastCellNum(); i++) {
127             csvData += getCellData(myRow.getCell(i));
128         }
129         csvData += NEW_LINE_CHARACTER;
130     }
131 
132             FileOutputStream writer = new FileOutputStream(csvFileName);
133             writer.write(csvData.getBytes());
134             writer.close();
135             return csvFileName;
136 }
137 
138 private static String getCellData(HSSFCell myCell){
139         String cellData = "";
140             if (myCell == null) {
141                 cellData += CVS_SEPERATOR_CHAR;
142             }
143 
144 
145             else if (myCell.getCellType() == CellType.BOOLEAN ) {
146                 cellData += myCell.getBooleanCellValue() + CVS_SEPERATOR_CHAR;
147             }
148 
149             else if (myCell.getCellType() == CellType.ERROR ) {
150                 cellData += myCell.getErrorCellValue() + CVS_SEPERATOR_CHAR;
151             }
152 
153             else if (myCell.getCellType() == CellType.FORMULA) {
154                 cellData += myCell.getCellFormula()+CVS_SEPERATOR_CHAR;
155                 }
156 
157 
158             else if (myCell.getCellType() == CellType.NUMERIC) {
159                 if ( DateUtil.isCellDateFormatted(myCell) ){
160                     cellData += new SimpleDateFormat(OUTPUT_DATE_FORMAT).format(myCell.getDateCellValue()) +CVS_SEPERATOR_CHAR;
161                 }else{
162                     cellData += myCell.getNumericCellValue()+CVS_SEPERATOR_CHAR ;
163                 }
164             }
165 
166             else if (myCell.getCellType() == CellType.STRING){
167                 cellData += myCell.getStringCellValue() + CVS_SEPERATOR_CHAR;
168             }
169             else {
170                 cellData += CVS_SEPERATOR_CHAR;;
171             }
172             return cellData;
173         }
174 
175 
176     public static void main(String[] args) throws IOException, InvalidFormatException {
177         transXlsx("D:\\work\\aaa\\test.xls","D:\\work\\aaa\\test.xlsx");
178     }
179 }

猜你喜欢

转载自www.cnblogs.com/DaveMo/p/12103450.html