我一直在尝试使用 HSSF 库从 SKL 文件中读取,我想从中读取并写入品牌 nes .XLS 文件
这是我的代码:
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
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 WriteReadExcel {
public static void main(String[] args) throws FileNotFoundException, IOException {
WriteReadExcel objhssf = new WriteReadExcel();
//objhssf.writeToExcel();
objhssf.readFromExcel();
}
public void writeToExcel() throws FileNotFoundException, IOException {
//create a work object
HSSFWorkbook wb = new HSSFWorkbook();
// create a work sheet
HSSFSheet wSheet = wb.createSheet("FirstHSSF");
//create a row
HSSFRow wRow = wSheet.createRow(0);
//create a column
HSSFCell wCell = wRow.createCell(0);
//write sth to this cell
wCell.setCellValue("Hello");
// save the file
wb.write(new FileOutputStream("SimpleHSSF.xls"));
//close the workbook
wb.close();
}
public void readFromExcel() throws FileNotFoundException, IOException {
// create an object of fileinputstream
FileInputStream file = new FileInputStream("FILE.xls");
// create workbook object
HSSFWorkbook wb = new HSSFWorkbook(file);
// refer to the worksheet
HSSFSheet wSheet = wb.getSheet("FirstHSSF");
// get the row
HSSFRow gRow = wSheet.getRow(0);
// get the column
HSSFCell gCell = gRow.getCell(0);
// get the value
String strValue = gCell.getStringCellValue();
// print the value
System.out.println(strValue);
//close
wb.close();
}
}
我不断收到此错误:无效的标头签名;读取 0x3B4C5857503B4449,预期为 0xE11AB1A1E011CFD0 - 您的文件似乎不是有效的 OLE2 文档
它似乎无法读取文件..
你能帮忙吗?您会注意到该文件具有 .xls 作为扩展名,但它不是 xls 文件,它是 sylk 。