Java 获取错误“;“无法读取整个标题”;通过ApachePOI创建文件时
问题:Java 获取错误“;“无法读取整个标题”;通过ApachePOI创建文件时,java,apache-poi,export-to-excel,xssf,poi-hssf,Java,Apache Poi,Export To Excel,Xssf,Poi Hssf,问题: 在xlwrite函数中,如果我取消创建文件的部分注释,则会出现上述异常。否则就可以了。为了实现我的目标,我应该在我的计划中改变什么 我不确定。我应该使用HSSF还是XSSF?这很好。现在我必须将Excel另存为.xls才能运行程序。它不接受.xlsx。我该怎么办 我已经创建了一个xl类来在Excel上执行读写操作,我将 从我的脚本调用这些函数。 我写的程序如下所示,但我得到一个例外- I am using Java and POI 3.7 and I want to perform f
我已经创建了一个xl类来在Excel上执行读写操作,我将 从我的脚本调用这些函数。 我写的程序如下所示,但我得到一个例外-
I am using Java and POI 3.7 and I want to perform following in xlWrite() function-
1. Check the file. If it doesn't exist create a new one.
If file exists, delete it and create a new one.
(DO I have to delete it or if it exists already my new content will overwrite it?)
2. Once check in 1. is completed, create a new sheet in the workbook if it doesn't exists.
封装lampsplus.utilities;
导入java.io.File;
导入java.io.FileInputStream;
导入java.io.FileOutputStream;
导入org.apache.poi.hssf.usermodel.HSSFCell;
导入org.apache.poi.hssf.usermodel.HSSFRow;
导入org.apache.poi.hssf.usermodel.HSSFSheet;
导入org.apache.poi.hssf.usermodel.HSSFWorkbook;
公共类操作{
//变量声明
私有int i,j,k;
私有int xRows、xCols;
私有字符串xlData[][];
私人住宅区;
专用HSSFCell单元;
私有文件;
流内私有文件输入流;
私有文件输出流外流;
私人HSSF工作手册;
私人HSSF表;
/**
*此方法返回从文件读取的XL。
*
*@return String[][]-读取数据的二维数组。
*/
公共字符串[][]getXLData()
{
返回数据;
}
/**
*类构造函数来初始化变量。
*
*/
公共业务()
{
System.out.println(“初始化构造函数dboperations:”);
这个.xlData=null;
this.row=null;
this.cell=null;
this.xlFile=null;
this.inStream=null;
this.outStream=null;
this.workbook=null;
this.sheet=null;
}
/**
*此方法用于读取二维数组中的excel文件。
*
*@param String-filePath-读取信息的文件路径。
*@param String-sheetName-要从文件中读取的工作表的名称。
*@返回无效
*@引发异常-意外异常
*/
public void xlRead(字符串文件路径、字符串sheetName)引发异常
{
文件文件=新文件(文件路径);
filePath=file.getAbsolutePath();
xlFile=新文件(文件路径);
inStream=新文件输入流(xlFile);
工作簿=新的HSSF工作簿(流程内);
sheet=工作簿.getSheet(sheetName);
xRows=sheet.getLastRowNum()+1;
xCols=sheet.getRow(0.getLastCellNum();
xlData=新字符串[xRows][xCols];
对于(i=0;i你的问题很难理解-你能修改格式吗?我已经更新了。现在很容易理解吗?如果文件存在并且你想替换它,为什么你要费心加载它并删除所有工作表?为什么不简单地在文件级别删除整个文件,然后总是重新创建它?谢谢Gagravarr。如果文件存在,我会我删除了整个文件并重新创建了一个新的。现在当我创建一个文件时,它工作了,但是我在创建后尝试读取它时遇到了异常。你知道我如何克服这个错误吗?它适用于所有其他人,并且适用于所有POI单元测试…你需要找出一个尽可能小的可行示例,以及最小的一个没有,并将其作为问题的更新发布。目前的代码太长,无法识别任何特定问题]
package lampsplus.utilities;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
public class XLOperations{
//variables declaration
private int i,j,k;
private int xRows, xCols;
private String xlData[][];
private HSSFRow row;
private HSSFCell cell;
private File xlFile;
private FileInputStream inStream;
private FileOutputStream outStream;
private HSSFWorkbook workbook;
private HSSFSheet sheet;
/**
* This method returns XL read from file.
*
* @return String[][] - two dimensional array of read data.
*/
public String[][] getXLData()
{
return xlData;
}
/**
* Class constructor to initialize variables.
*
*/
public void XLOperations()
{
System.out.println("initializing constructor dboperations:");
this.xlData= null;
this.row = null;
this.cell = null;
this.xlFile = null;
this.inStream = null;
this.outStream = null;
this.workbook = null;
this.sheet = null;
}
/**
* This method is for reading a excel file in two dimensional array.
*
* @param String - filePath - Path of the file from where to read the information.
* @param String - sheetName -Name of the sheet to read from file.
* @return void
* @throws Exception - An unexpected exception
*/
public void xlRead(String filePath,String sheetName) throws Exception
{
File file = new File(filePath);
filePath= file.getAbsolutePath();
xlFile = new File(filePath);
inStream = new FileInputStream(xlFile);
workbook = new HSSFWorkbook(inStream);
sheet = workbook.getSheet(sheetName);
xRows = sheet.getLastRowNum()+1 ;
xCols = sheet.getRow(0).getLastCellNum();
xlData = new String[xRows][xCols];
for(i=0;i<xRows;i++)
{
row = sheet.getRow(i);
for(j=0;j<xCols;j++)
{
cell = row.getCell(j);
String value = cellToString(cell);
xlData[i][j] = value;
}
}
}
/**
* This method is for writing a writing a excel sheet into a file.
*
* @param String - filePath
* @param String - sheetname
* @param string - two dimensional array to write in the file
* @return void
* @throws Exception - An unexpected exception
*/
public void xlWrite(String filePath,String sheetName,String data[][]) throws Exception
{
File file = new File(filePath);
filePath= file.getAbsolutePath();
xlFile = new File(filePath);
/* if(!xlFile.exists())
{
System.out.println("Created file");
xlFile.createNewFile();
}
*/
inStream = new FileInputStream(xlFile);
workbook = new HSSFWorkbook(inStream);
k = workbook.getSheetIndex(sheetName);
if(k!=-1)
{
workbook.removeSheetAt(k);
}
HSSFSheet sheet = workbook.createSheet(sheetName);
FileOutputStream fOut = new FileOutputStream(xlFile);
xRows = data.length;
xCols = data[0].length;
for(i =0;i<xRows;i++)
{
row = sheet.createRow(i);
for(j=0;j<xCols;j++)
{
cell = row.createCell(j);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setCellValue(data[i][j]);
}
}
workbook.write(fOut);
fOut.flush();
fOut.close();
}
public String cellToString(HSSFCell cell)
{
int type = cell.getCellType();
Object result;
switch(type)
{
case HSSFCell.CELL_TYPE_NUMERIC: //0
result = cell.getNumericCellValue();
break;
case HSSFCell.CELL_TYPE_STRING://1
result = cell.getStringCellValue();
break;
case HSSFCell.CELL_TYPE_FORMULA://2
throw new RuntimeException("Java can't evaluate formulas");
case HSSFCell.CELL_TYPE_BLANK://3
result = "";
break;
case HSSFCell.CELL_TYPE_BOOLEAN://4
result = cell.getBooleanCellValue();
break;
case HSSFCell.CELL_TYPE_ERROR://5
throw new RuntimeException("This cell has an error");
default:
throw new RuntimeException("We don't support this cell type:" + type);
}
return result.toString();
}
}
java.io.IOException: Unable to read entire header; 0 bytes read; expected 32 bytes
at org.apache.poi.poifs.storage.HeaderBlockReader.alertShortRead(HeaderBlockReader.java:176)
at org.apache.poi.poifs.storage.HeaderBlockReader.<init>(HeaderBlockReader.java:103)
at org.apache.poi.poifs.filesystem.POIFSFileSystem.<init>(POIFSFileSystem.java:153)
at org.apache.poi.hssf.usermodel.HSSFWorkbook.<init>(HSSFWorkbook.java:305)
at org.apache.poi.hssf.usermodel.HSSFWorkbook.<init>(HSSFWorkbook.java:286)
at tryIng.xlWrite(tryIng.java:74)
at tryIng.main(tryIng.java:49)