[英]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.