简体   繁体   English

如何为使用WorkbookFactory创建的工作簿创建CellStyle?

[英]How to create CellStyle for workbook that is created using WorkbookFactory?

I am working on a java code where I have to compare two excel sheets regardless of the format ( xls or xlsx) and copy the rows that differ to a new excel sheet. 我正在研究一个Java代码,其中无论格式如何(xls或xlsx),我都必须比较两个Excel工作表并将不同的行复制到新的Excel工作表中。 Then I read about WorkbookFactory which would accept both format. 然后,我读到了接受两种格式的WorkbookFactory。 Below is the code snippet I created : Please help!! 下面是我创建的代码片段:请帮助!

    import org.apache.poi.ss.usermodel.Workbook;
    import org.apache.poi.ss.usermodel.WorkbookFactory;
    import org.apache.poi.xssf.usermodel.XSSFWorkbook;
    import org.apache.poi.ss.usermodel.CellStyle;
    import org.apache.poi.ss.usermodel.IndexedColors;

    class Compare extends CopyRow
    {   
     //<-------VARIABLES DECLARATION & INITIALISATION----->

      Workbook outputWorkbook,workbook1,workbook2;
 Sheet excel1Sheet,excel2Sheet,newSheet;

       public void compare(String fileA,String fileB)

         {
    try
    {
      if(fileA.endsWith(".xls")outputWorkbook = new XSSFWorkbook(new FileInputStream("C:/Documents and Settings/eclipse/workspace/CompareExcelV2/output.xls"));
else
outputWorkbook = new XSSFWorkbook(new FileInputStream("C:/Documents and Settings/eclipse/workspace/CompareExcelV2/output.xlsx"))
      workbook1 = WorkbookFactory.create(new File(fileA));
      workbook2 = WorkbookFactory.create(new File(fileB));

      CellStyle excel1Red = new workbook1.createCellStyle(); **//ERROR HERE**
 CellStyle excel2Green = new workbook2.createCellStyle(); **//ERROR HERE**
      CellStyle newStyle = new outputWorkbook.createCellStyle();**//ERROR HERE**

     }
   catch {}

} }

I am not able to create CellStyle. 我无法创建CellStyle。 I get the following error: 我收到以下错误:

workbook1 cannot be resolved to a type

workbook2 cannot be resolved to a type

outputWorkbook cannot be resolved to a type

If we take your code: 如果我们采用您的代码:

if(fileA.endsWith(".xls")outputWorkbook = new XSSFWorkbook(new FileInputStream("C:/Documents and Settings/eclipse/workspace/CompareExcelV2/output.xls"));
else outputWorkbook = new XSSFWorkbook(new FileInputStream("C:/Documents and Settings/eclipse/workspace/CompareExcelV2/output.xlsx"))
workbook1 = WorkbookFactory.create(new File(fileA));
workbook2 = WorkbookFactory.create(new File(fileB));

Then there's a few things. 然后有几件事。 One is that opening from a File is lower memory than from a stream , the second is that your code doesn't seem to be doing what you want. 一是从文件打开比从流打开的内存少 ,二是您的代码似乎没有执行您想要的操作。 You appear to be opening outputWorkbook based on a file, when I suspect you just want a new empty one that you later save. 您似乎在基于文件打开outputWorkbook时,我怀疑您只是想要一个新的空文件,以后再保存。 Instead, how about something like: 相反,怎么样:

final String path = "C:/Documents and Settings/eclipse/workspace/CompareExcelV2/";
Workbook outputWorkbook;
FileOutputStream outputTo;
if (fileA.endsWith(".xls") {
   outputWorkbook = new HSSFWorkbook();
   outputTo = new FileOutputStream(path + "output.xls");
} else {
   outputWorkbook = new XSSFWorkbook();
   outputTo = new FileOutputStream(path + "output.xlsx");
}
Workbook workbook1 = WorkbookFactory.create(new File(fileA));
Workbook workbook2 = WorkbookFactory.create(new File(fileB));

Finally, make sure you have all the right jars on your classpath, see the Components and their Dependencies section on the POI website for details 最后,确保在类路径上具有所有正确的jar,有关详细信息,请参见POI网站上的“组件及其依赖项”部分

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用apache poi 4.1.0创建一个带有`WorkbookFactory`的工作簿 - how to create a workbook with `WorkbookFactory` using apache poi 4.1.0 WorkbookFactory.create(的inputStream) - WorkbookFactory.create(inputStream) 有没有办法知道CellStyle已经存在于工作簿中(重用)使用POI或仅复制Celstyle obj而不是参考 - Is there any way to know that the CellStyle is already present in Workbook(to reuse) using POI or to Copy only Celstyle obj not reference Apache POI从工作簿中删除CellStyle - Apache POI delete CellStyle from workbook 如何使用Java在工作簿中创建Excel工作表? - how to create excel sheet in workbook using java? WorkbookFactory.create(inputstream)抛出StringIndexOutOfBounds异常 - WorkbookFactory.create(inputstream) throws StringIndexOutOfBounds exception Apache Poi WorkbookFactory.create()的性能问题 - Apache Poi performance issue with WorkbookFactory.create() 如何将CellStyle对象存储到数据库中? - How to store CellStyle object into database? cloneStyleFrom 在使用 java poi 处理 cellStyle 时显示 ArrayIndexOutOfBound 错误,如何解决此问题 - cloneStyleFrom shows ArrayIndexOutOfBound error while coping cellStyle using java poi, how to fix this issue Apache POI,处理 WorkbookFactory.create() 异常以获得更好的用户体验 - Apache POI, Handling WorkbookFactory.create() exception for better user experience
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM