从格式的Excel文件中获取单元格值

我试图追加所有的内容从一个Excel文件表到另一个Excel文件表。 一切都好,除了单元格的格式。

从原来的Excel文件采取相同的格式,我使用HSSFCellStyle

这是我的代码:

私人申报:

 private HSSFRow row1; private HSSFCell cell1; private HSSFCellStyle cellStyle1; private FileInputStream inFile1,inFile2; private HSSFSheet excelSheet1=null,excelSheet2=null; private HSSFWorkbook excelBook1=null,excelBook2=null; 

主要方法:

 public static void main(String args[]){ appendToExcelClass test = new appendToExcelClass(); test.appendToExcel(new File("C:\\excel1.xls"),new File("C:\\excel2.xls")); } 

附加内容的方法:

 public void appendToExcel(File file1,File file2){ try{ if(file1.exists() && file2.exists()){ inFile1 = new FileInputStream(file1); inFile2 = new FileInputStream(file2); excelBook1 = new HSSFWorkbook(inFile1); excelBook2 = new HSSFWorkbook(inFile2); excelSheet1 = excelBook1.getSheetAt(0); excelSheet2 = excelBook2.getSheetAt(0); Iterator rowIter = excelSheet2.rowIterator(); while(rowIter.hasNext()){ HSSFRow myRow = (HSSFRow) rowIter.next(); Iterator cellIter = myRow.cellIterator(); List<String> cellStoreVector; cellStoreVector = new ArrayList<>(); while(cellIter.hasNext()){ HSSFCell myCell = (HSSFCell) cellIter.next(); String cellvalue = myCell.getStringCellValue(); cellStyle1 = myCell.getCellStyle(); /*The problem is in this part, I think I didn't get well how get the cell's format*/ cellStoreVector.add(cellvalue); } row1 = excelSheet1.createRow(excelSheet1.getLastRowNum()+1); cell1 = row1.createCell(0); cell1.setCellStyle(cellStyle1); /*At the moment to execute this part, it throws this: This Style does not belong to the supplied Workbook. Are you trying to assign a style from one workbook to the cell of a different workbook?*/ cell1.setCellValue(cellStoreVector.get(0).toString()); } FileOutputStream outFile1 = new FileOutputStream(file1); excelBook1.write(outFile1); outFile1.close(); } }catch(Exception ex){ ex.printStackTrace(); } } 

我不确定那个把错误抛给我的部分。

提前,谢谢。

错误信息几乎解释了这个问题。 HSSFWorkbook包含一个可以使用的所有样式的表格。 每当您调用setCellStyle时,您传入的HSSFCellStyle必须位于该表中。 发生了什么事是你在excelBook2中从单元中提取的HSSFCellStyle在excelBook1中不存在。

要解决这个问题,你可以调用excelBook1.createCellStyle来创build一个新的样式,并从提取的样式中克隆它的属性。 这是一个如何做到这一点的例子。

 HSSFCellStyle newStyle = excelBook1.createCellStyle(); newStyle.cloneStyleFrom(cellStyle1); 

我发布这第二个答案,因为我认为第一个答案,虽然不完整,是有用的。 基本问题是我在第一个答案中确定的 – 每个工作簿都有一个关联的样式表,并且您必须确保excelBook2中的所有样式都存在于excelBook1中。

然而,最大的问题是,你如何知道excelBook1中的哪些样式从excelBook1中丢失? 以下是两个更详细地解释问题的链接:

为什么克隆的HSSFCellStyle不等于克隆的样式?

http://apache-poi.1045710.n5.nabble.com/HSSFCellStyle-help-td2295062.html

现在让我来概述一下我为了解决这个问题所做的一些build议。

a)用下面的脚印写一个方法:

 public boolean stylesAreEquivalent(HSSFCellStyle style1, HSSFCellStyle style2) 

在此方法中,将style1中的每个属性与style2中的相同属性进行比较。 这包括alignment,边框样式,各种颜色的rgb值,字体名称,字体大小等等。 如果两个样式中的所有基础属性都相同,则该方法将返回true。

b)创build下面的HashMap

 HashMap<short,HSSFCellStyle> book2Styles = new HashMap<short,HSSFCellStyle>(); 

这张地图将被从HSSFCellStyle.getIndex()获得的值索引。

c)从想要从book2复制到book1的单元格中获取样式。 使用getIndex获取索引。 执行以下代码:

 HSSFCellStyle styleFromMap = book2Styles.get(index); 

如果styleFromMap不为空,则转到下面的步骤d)。

如果styleFromMap为null,那么你需要做的是找出book1中是否有相同的风格,你可以使用。 您可以通过调用book1中每个样式的stylesAreEquivalent方法来查看是否有任何匹配。 如果匹配,则执行以下代码:

 book2Styles.put(index, equivalentBook1Style); styleFromMap = equivalentBook1Style; 

如果没有相应的风格,那么你需要创build一个新的风格,并使用我以前的答案中的代码克隆它。

 HSSFCellStyle newStyle = excelBook1.createCellStyle(); newStyle.cloneStyleFrom(cellStyle1); book2Styles.put(index, newStyle); styleFromMap = newStyle; 

d)使用styleFromMap中的样式将book2中的单元复制到book1。

使用该方法可以最大限度地减lessbook1中必须创build的新样式的数量。 由于有400种风格的限制,这一点很重要。

我希望有一个更简单的方法,但这绝对是可行的,不会是那么多的代码。