嗨,我想阅读xlsx文件或xls文件,它是什么.XSSF可以支持xls文件吗?或者我是否需要为这两种文件编写单独的代码?
我正在尝试使用POI XSSF来评估一些Excel公式.这些值不必保存,我可能需要计算很多公式,所以我试图在同一个单元格中完成所有这些.
问题是即使在我重新计算之后,单元格值似乎仍然卡在输入的第一个公式上
FormulaEvaluator evaluator = wb.getCreationHelper().createFormulaEvaluator();
XSSFCell formulaCell = row.createCell(6);
formulaCell.setCellFormula("Date(2011,10,6)");
CellValue cellValue = evaluator.evaluate(formulaCell);
System.out.println(cellValue.getNumberValue());
formulaCell.setCellFormula("Date(1911,3,4)");
cellValue = evaluator.evaluate(formulaCell);
System.out.println(cellValue.getNumberValue());
Run Code Online (Sandbox Code Playgroud)
这两次输出40822.0 40822.0(excel相当于10/6/2011),而不是重新评估新公式.
我正在使用Apache POI的事件API读取XLSX文件,即我通过SAX Parser读取XLSX表的内容.我想知道如何通过使用XSSF事件API获取公式的计算值.
我知道这样做的方法是使用FormulaEvaluator该类.但是,由于formulaEvaluator采用Workbook类的实例,我不想使用这种方法.(我正在阅读包含一百万行和100列的Excel文件,因此如果我创建该Excel的工作簿对象,我的应用服务器内存不足,因此我使用的是事件API)
如果没有Workbook实例,如何在事件解析中进行评估?
我正在尝试使用Apache POI在Excel文件中创建工作表.
由于它是Excel 2007,我正在使用XSSF,我正在寻找一种方法来使表格从右到左对齐.
在HSSF中有一种方法org.apache.poi.hssf.usermodel.HSSFSheet.setRightToLeft(boolean),但我找不到它org.apache.poi.xssf.usermodel.XSSFSheet.
我正在使用Apache POI 3.7
我正在处理现有的.xlsx文件.任何人都可以与我分享,我怎样才能得到我当前的文件名?
我的意思是,假设我正在使用test.xlsx文件.如何使用apache poi获取工作簿"test.xlsx"的名称.
打开并立即关闭使用 Apache POI XSSF 创建的 xlsx 文件后,系统会提示我保存未保存的更改。据我所知,发生这种情况是因为我在 xlsx 文件中使用公式。
根据javadoc,这应该通过设置来绕过XSSFWorkbook.setForceFormulaRecalculation(true)
但是,这并不能解决问题。
我还尝试在保存文件之前手动重新计算公式,但没有成功。
SSCCE:
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFFormulaEvaluator;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class XSSFExample {
public static void main(String[] args) {
// Create workbook and sheet
Workbook wb = new XSSFWorkbook();
Sheet sheet = wb.createSheet("Sheet 1");
// Create a row and put some cells in it.
Row row = sheet.createRow((short) 0);
row.createCell(0).setCellValue(5.0);
row.createCell(1).setCellValue(5.0);
row.createCell(2).setCellFormula("A1/B1");
// Write the output to a file
try (FileOutputStream …Run Code Online (Sandbox Code Playgroud) 我正在开发一个应用程序,该应用程序获取数据库记录并从该数据创建 Excel 文档。
excel文档生成良好,所有数据可读;截至本论坛之前的回答,该表也已正确生成(即使我滚动过去,标题行仍然可见,因此该表肯定存在)。但是,我原以为一旦有了表格,我就能够对列进行排序和过滤,就像在 Excel 中“插入 -> 表”时的情况一样,但是当我打开文档时没有这样的选项。
我在 XSSFTable 或 XSSFTableColumn 类上没有看到 setFitlerable 或 setSortable 或类似的内容...如何在表列上启用排序/过滤?
如果有用的话,表创建代码如下:
//Create table
CellReference topLeft = new CellReference(sheet.getRow(3).getCell(0));
CellReference bottomRight = new CellReference(sheet.getRow(nextRow-1).getCell(3));
AreaReference tableArea = workbook.getCreationHelper().createAreaReference(topLeft, bottomRight);
XSSFTable dataTable = sheet.createTable(tableArea);
dataTable.setName("TableData" + EXCEL_OBJECT_NUMBER);
dataTable.setDisplayName("TableData" + EXCEL_OBJECT_NUMBER);
XSSFTableColumn column = dataTable.getColumns().get(0);
column.setId(1);
column.setName("COLUMN1");
column = dataTable.getColumns().get(1);
column.setId(2);
column.setName("COLUMN2");
column = dataTable.getColumns().get(2);
column.setId(3);
column.setName("COLUMN3");
column = dataTable.getColumns().get(3);
column.setId(4);
column.setName("COLUMN4");
Run Code Online (Sandbox Code Playgroud) 我试图检查我的excel文件是否已经存在.如果它不存在,我想创建一个新的,如果它存在,我将删除它并创建一个新的.我编写了以下程序但是我遇到了错误 - workbook = WorkbookFactory.create(instream);
错误是 - > java.lang.IllegalArgumentException:您的InputStream既不是OLE2流,也不是tryIng.main上的org.apache.poi.ss.usermodel.WorkbookFactory.create(WorkbookFactory.java:89)中的OOXML流(tryIng)的.java:84)
这是一个程序 - >
try {
String filePath= "C:/Users/pritik/Desktop/t1.xlsx";
File file = new File(filePath);
filePath= file.getAbsolutePath();
xlFile = new File(filePath);
if(xlFile.exists() && !xlFile.isDirectory())
xlFile.delete(); //delete if file already exists.
xlFile.createNewFile();
inStream = new FileInputStream(xlFile);
workbook = WorkbookFactory.create(inStream); // I get error at this line
String sheetName="NewSheet";
Sheet sheet = workbook.createSheet(sheetName);
FileOutputStream fOut = new FileOutputStream(xlFile);
int i,j;
xRows = xTS.length;
xCols = xTS[0].length;
for(i =0;i<xRows;i++)
{
row = sheet.createRow(i);
for(j=0;j<xCols;j++)
{
cell …Run Code Online (Sandbox Code Playgroud) 在Java方法中,我使用的是Apache POI Sheet(来自XSSFWorkbook).我可以使用getSheetName()方法阅读工作表名称.但有没有办法找到工作簿中的工作表位置.我没有getSheetIndex()在Sheet界面中看到任何方法.
我试图包含引用库中所需的所有 jar。这个论坛上有类似的问题,我已经解决了所有问题,但无法解决问题。
我的代码片段:
Workbook workbook = new XSSFWorkbook();
CreationHelper createHelper = workbook.getCreationHelper();
Sheet sheet = workbook.createSheet("Gene");
Font headerFont = workbook.createFont();
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
异常堆栈跟踪:
org.apache.poi.ooxml.POIXMLException: org.apache.poi.ooxml.POIXMLException: java.lang.reflect.InvocationTargetException
at org.apache.poi.ooxml.POIXMLDocumentPart.createRelationship(POIXMLDocumentPart.java:602)
at org.apache.poi.xssf.usermodel.XSSFWorkbook.createSheet(XSSFWorkbook.java:896)
at org.apache.poi.xssf.usermodel.XSSFWorkbook.createSheet(XSSFWorkbook.java:807)
at org.apache.poi.xssf.usermodel.XSSFWorkbook.createSheet(XSSFWorkbook.java:122)
at DomParser.main(DomParser.java:18)
Caused by: org.apache.poi.ooxml.POIXMLException: java.lang.reflect.InvocationTargetException
at org.apache.poi.ooxml.POIXMLFactory.newDocumentPart(POIXMLFactory.java:111)
at org.apache.poi.ooxml.POIXMLDocumentPart.createRelationship(POIXMLDocumentPart.java:587)
... 4 more
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at org.apache.poi.xssf.usermodel.XSSFFactory.createDocumentPart(XSSFFactory.java:56)
at org.apache.poi.ooxml.POIXMLFactory.newDocumentPart(POIXMLFactory.java:109)
... 5 more
Caused by: java.lang.NoSuchMethodError: org.openxmlformats.schemas.spreadsheetml.x2006.main.impl.CTWorksheetImpl.generatedSetterHelperImpl(Lorg/apache/xmlbeans/XmlObject;Ljavax/xml/namespace/QName;IS)Lorg/apache/xmlbeans/XmlObject;
at org.openxmlformats.schemas.spreadsheetml.x2006.main.impl.CTWorksheetImpl.setColsArray(Unknown Source)
at org.apache.poi.xssf.usermodel.helpers.ColumnHelper.cleanColumns(ColumnHelper.java:66)
at org.apache.poi.xssf.usermodel.helpers.ColumnHelper.<init>(ColumnHelper.java:46) …Run Code Online (Sandbox Code Playgroud) xssf ×10
apache-poi ×8
java ×8
excel ×4
xlsx ×2
apache ×1
excel-tables ×1
hssf ×1
poi-hssf ×1
xls ×1