From 986aa62ed00bee39363bab41b4eeb8259d446efd Mon Sep 17 00:00:00 2001 From: ludc <ludc@vci-tech.com> Date: 星期四, 16 一月 2025 18:20:32 +0800 Subject: [PATCH] Merge remote-tracking branch 'origin/master' --- Source/Client/PLTClientBase/src/com/vci/client/common/excel/ExcelDocumentUtils.java | 177 ++++++++++++++++++++++++++-------------------------------- 1 files changed, 79 insertions(+), 98 deletions(-) diff --git a/Source/Client/PLTClientBase/src/com/vci/client/common/excel/ExcelDocumentUtils.java b/Source/Client/PLTClientBase/src/com/vci/client/common/excel/ExcelDocumentUtils.java index cbcf1d9..5a8e147 100644 --- a/Source/Client/PLTClientBase/src/com/vci/client/common/excel/ExcelDocumentUtils.java +++ b/Source/Client/PLTClientBase/src/com/vci/client/common/excel/ExcelDocumentUtils.java @@ -8,14 +8,11 @@ import java.io.IOException; import java.io.InputStream; import java.text.DecimalFormat; -import java.text.NumberFormat; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Arrays; import java.util.Date; import java.util.List; -import java.util.Locale; - import org.apache.poi.hssf.usermodel.HSSFDateUtil; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.poifs.filesystem.POIFSFileSystem; @@ -29,22 +26,22 @@ public class ExcelDocumentUtils { /** - *鍐欏洖鍒癳xcel鏂囨。 + * 鍐欏洖鍒癳xcel鏂囨。 * * @param sheetDataSet - * <P> - * 鏁版嵁闆嗗悎 + * <P> + * 鏁版嵁闆嗗悎 * @param path - * <P> - * excel鏂囨。鐨勮矾寰� + * <P> + * excel鏂囨。鐨勮矾寰� * @param name - * <P> - * excel鏂囨。鐨勫悕绉� + * <P> + * excel鏂囨。鐨勫悕绉� * @throws VCIError - * @throws IOException - * */ - public static void writeExcelDocument(String path, String name, - List<SheetDataSet> sheetDataSet) throws VCIError, IOException { + * @throws IOException + */ + public static void writeExcelDocument(String path, String name, List<SheetDataSet> sheetDataSet) + throws VCIError, IOException { // 鎵撳紑宸ヤ綔绨� Workbook workBook = makeWorkbook(name); // sheet @@ -64,25 +61,24 @@ row = sheet.createRow(rowIndex); } String[] rowData = rowDataSet.get(rowIndex); - //row + // row for (int columnIndex = 0; columnIndex < rowData.length; columnIndex++) { // cell Cell cell = row.getCell(columnIndex); if (cell == null) { cell = row.createCell(columnIndex); } - //璧嬛� + // 璧嬛� cell.setCellValue(rowData[columnIndex]); } } - }// end + } // end FileOutputStream fileOutputStream = makeFileOutputStream(path, name); workBook.write(fileOutputStream); fileOutputStream.close(); } - - public static void writeExcelDocument(String path, String name, - SheetDataSet dataSet) throws VCIError, IOException { + + public static void writeExcelDocument(String path, String name, SheetDataSet dataSet) throws VCIError, IOException { // 鎵撳紑宸ヤ綔绨� Workbook workBook = makeWorkbook(name); // sheet @@ -101,14 +97,14 @@ row = sheet.createRow(rowIndex); } String[] rowData = rowDataSet.get(rowIndex); - //row + // row for (int columnIndex = 0; columnIndex < rowData.length; columnIndex++) { // cell Cell cell = row.getCell(columnIndex); if (cell == null) { cell = row.createCell(columnIndex); } - //璧嬛� + // 璧嬛� cell.setCellValue(rowData[columnIndex]); } } @@ -117,9 +113,9 @@ workBook.write(fileOutputStream); fileOutputStream.close(); } - - - public static List<SheetDataSet> readExcelDocument(String name,InputStream fileInputStream) throws VCIError, IOException { + + public static List<SheetDataSet> readExcelDocument(String name, InputStream fileInputStream) + throws VCIError, IOException { return readExcelDocument(name, fileInputStream, false); } @@ -127,28 +123,27 @@ * 璇诲彇excel鏂囨。鐨勬暟鎹� * * @param FileInputStream - * <P> - * excel鏂囨。 + * <P> + * excel鏂囨。 * @param name - * <P> - * excel鏂囨。鐨勫悕绉� - * @throws IOException - * */ - public static List<SheetDataSet> readExcelDocument(String name,InputStream fileInputStream, boolean isDelNullRow) + * <P> + * excel鏂囨。鐨勫悕绉� + * @throws IOException + */ + public static List<SheetDataSet> readExcelDocument(String name, InputStream fileInputStream, boolean isDelNullRow) throws VCIError, IOException { InputStream bufferInputStream = getBufferInputStream(fileInputStream); // 鎵撳紑宸ヤ綔绨� - Workbook workBook = getWorkbook(fileInputStream, bufferInputStream, - name); + Workbook workBook = getWorkbook(fileInputStream, bufferInputStream, name); // row - int maxRow=0; + int maxRow = 0; List<SheetDataSet> dataSet = new ArrayList<SheetDataSet>(); for (int sheetIndex = 0; sheetIndex < workBook.getNumberOfSheets(); sheetIndex++) { Sheet sheet = workBook.getSheetAt(sheetIndex); SheetDataSet sheetDataSet = new SheetDataSet(); sheetDataSet.setSheet(sheet.getSheetName()); dataSet.add(sheetDataSet); - //閬嶅巻 + // 閬嶅巻 for (int rowIndex = 0; rowIndex <= sheet.getLastRowNum(); rowIndex++) { Row rowDataSet = sheet.getRow(rowIndex); @@ -160,16 +155,15 @@ continue; } int currentcolumnSize = rowDataSet.getLastCellNum(); - if(maxRow<currentcolumnSize){ - maxRow=currentcolumnSize; + if (maxRow < currentcolumnSize) { + maxRow = currentcolumnSize; } - // + // String[] values = new String[maxRow]; Arrays.fill(values, ""); boolean hasValue = false; - //閬嶅巻 - for (int columnIndex = 0; columnIndex < rowDataSet - .getLastCellNum(); columnIndex++) { + // 閬嶅巻 + for (int columnIndex = 0; columnIndex < rowDataSet.getLastCellNum(); columnIndex++) { String value = getCellValue(columnIndex, rowDataSet); values[columnIndex] = rightTrim(value); hasValue = true; @@ -183,34 +177,31 @@ return dataSet; } - /** * 璇诲彇excel鏂囨。鐨勬暟鎹� * * @param path - * <P> - * excel鏂囨。鐨勮矾寰� + * <P> + * excel鏂囨。鐨勮矾寰� * @param name - * <P> - * excel鏂囨。鐨勫悕绉� - * @throws IOException - * */ - public static List<SheetDataSet> readExcelDocument(String path, String name) - throws VCIError, IOException { + * <P> + * excel鏂囨。鐨勫悕绉� + * @throws IOException + */ + public static List<SheetDataSet> readExcelDocument(String path, String name) throws VCIError, IOException { FileInputStream fileInputStream = getFileInputStream(path, name); InputStream bufferInputStream = getBufferInputStream(fileInputStream); // 鎵撳紑宸ヤ綔绨� - Workbook workBook = getWorkbook(fileInputStream, bufferInputStream, - name); + Workbook workBook = getWorkbook(fileInputStream, bufferInputStream, name); // row - int maxRow=0; + int maxRow = 0; List<SheetDataSet> dataSet = new ArrayList<SheetDataSet>(); for (int sheetIndex = 0; sheetIndex < workBook.getNumberOfSheets(); sheetIndex++) { Sheet sheet = workBook.getSheetAt(sheetIndex); SheetDataSet sheetDataSet = new SheetDataSet(); sheetDataSet.setSheet(sheet.getSheetName()); dataSet.add(sheetDataSet); - //閬嶅巻 + // 閬嶅巻 for (int rowIndex = 0; rowIndex <= sheet.getLastRowNum(); rowIndex++) { Row rowDataSet = sheet.getRow(rowIndex); @@ -219,16 +210,15 @@ continue; } int currentcolumnSize = rowDataSet.getLastCellNum(); - // - if(maxRow<currentcolumnSize){ - maxRow=currentcolumnSize; + // + if (maxRow < currentcolumnSize) { + maxRow = currentcolumnSize; } String[] values = new String[maxRow]; Arrays.fill(values, ""); boolean hasValue = false; - //閬嶅巻 - for (int columnIndex = 0; columnIndex < rowDataSet - .getLastCellNum(); columnIndex++) { + // 閬嶅巻 + for (int columnIndex = 0; columnIndex < rowDataSet.getLastCellNum(); columnIndex++) { String value = getCellValue(columnIndex, rowDataSet); if (columnIndex == 0 && value.trim().equals("")) { break; @@ -245,7 +235,6 @@ return dataSet; } - private static Workbook makeWorkbook(String name) throws VCIError { // 鍒涘缓 @@ -259,58 +248,53 @@ } - private static FileOutputStream makeFileOutputStream(String path, - String name) throws VCIError, FileNotFoundException { - FileOutputStream fileOutputStream = new FileOutputStream(path + "\\" - + name); + private static FileOutputStream makeFileOutputStream(String path, String name) + throws VCIError, FileNotFoundException { + FileOutputStream fileOutputStream = new FileOutputStream(path + "\\" + name); return fileOutputStream; } - private static FileInputStream getFileInputStream(String path, String name) - throws VCIError, FileNotFoundException { + private static FileInputStream getFileInputStream(String path, String name) throws VCIError, FileNotFoundException { File xlsFile = new File(path + "\\" + name); FileInputStream fileInputStream = new FileInputStream(xlsFile); return fileInputStream; } - private static BufferedInputStream getBufferInputStream( - InputStream fileInputStream) throws VCIError { - BufferedInputStream bufferInputStream = new BufferedInputStream( - fileInputStream); + private static BufferedInputStream getBufferInputStream(InputStream fileInputStream) throws VCIError { + BufferedInputStream bufferInputStream = new BufferedInputStream(fileInputStream); return bufferInputStream; } /** * 璇诲彇鍗曞厓鏍肩殑鍊� - * */ + */ private static String getCellValue(int column, Row row) { return getCellValue(row.getCell(column)); } - - - public static String getCellValue(Cell cell){ + + public static String getCellValue(Cell cell) { String cellValueString = ""; - if(cell == null) return cellValueString; + if (cell == null) + return cellValueString; int cellType = cell.getCellType(); SimpleDateFormat sdfDateTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); // add by xchao 2012.11.22 澶勭悊鏃ユ湡绫诲瀷鍜孋ell - if(cellType == Cell.CELL_TYPE_NUMERIC && HSSFDateUtil.isCellDateFormatted(cell)){ + if (cellType == Cell.CELL_TYPE_NUMERIC && HSSFDateUtil.isCellDateFormatted(cell)) { Date date = HSSFDateUtil.getJavaDate(cell.getNumericCellValue()); cellValueString = sdfDateTime.format(date); - } - else if(cellType == Cell.CELL_TYPE_BLANK){ + } else if (cellType == Cell.CELL_TYPE_BLANK) { cellValueString = ""; - } else if(cellType == Cell.CELL_TYPE_BOOLEAN){ + } else if (cellType == Cell.CELL_TYPE_BOOLEAN) { cellValueString = cell.getBooleanCellValue() ? "1" : "0"; - } else if(cellType == Cell.CELL_TYPE_ERROR){ + } else if (cellType == Cell.CELL_TYPE_ERROR) { cellValueString = ""; - } else if(cellType == Cell.CELL_TYPE_FORMULA){ - try{ + } else if (cellType == Cell.CELL_TYPE_FORMULA) { + try { cellValueString = String.valueOf(cell.getNumericCellValue()); } catch (Exception e) { cellValueString = cell.getRichStringCellValue().getString(); } - } else if(cellType == Cell.CELL_TYPE_NUMERIC){ + } else if (cellType == Cell.CELL_TYPE_NUMERIC) { // String[] dataFormatStrings = cell.getCellStyle().getDataFormatString().replace("_", "").trim().split("\\."); // int maxFracDigits = 0; // if(dataFormatStrings.length == 2){ @@ -320,23 +304,23 @@ // } else { // cellValueString = cell.toString(); // } - //cell.setCellType(Cell.CELL_TYPE_STRING); - //cellValueString = cell.getStringCellValue(); + // cell.setCellType(Cell.CELL_TYPE_STRING); + // cellValueString = cell.getStringCellValue(); DecimalFormat df = new DecimalFormat("#.#########"); cellValueString = df.format(cell.getNumericCellValue()); - } else if(cellType == Cell.CELL_TYPE_STRING){ + } else if (cellType == Cell.CELL_TYPE_STRING) { // 绉婚櫎鏁版嵁鍒椾腑鍓嶅彲鍦ㄥ瓨鍦ㄧ殑"'" String val = cell.getStringCellValue(); - while(val.startsWith("'")){ + while (val.startsWith("'")) { val = val.substring(1); } cellValueString = val; - } + } return cellValueString; } - private static Workbook getWorkbook(InputStream fileInputStream, - InputStream bufferInputStream, String name) throws VCIError, IOException { + private static Workbook getWorkbook(InputStream fileInputStream, InputStream bufferInputStream, String name) + throws VCIError, IOException { // 鎵撳紑宸ヤ綔绨� Workbook workBook = null; @@ -357,7 +341,7 @@ /** * 去锟斤拷锟街凤拷锟揭边的空革拷 - * */ + */ private static String rightTrim(String value) { if (value == null) { return ""; @@ -372,17 +356,14 @@ return value.substring(0, length); } - public static void main(String args[]) throws IOException { try { - List<SheetDataSet> sheetDataSet = readExcelDocument("D:\\", - "a.xlsx"); + List<SheetDataSet> sheetDataSet = readExcelDocument("D:\\", "a.xlsx"); writeExcelDocument("D:\\", "b.xlsx", sheetDataSet); } catch (VCIError e) { // TODO Auto-generated catch block e.printStackTrace(); } } - - + } -- Gitblit v1.9.3