JAVA签名已损坏,NotOLE2FileException:无效的标头签名;读取0x0000000000000000,预期为0xE11AB1A1E011CFD0...

What I want to do is ask the user if they want to create a new or select an existing Excel workbook. Selecting an existing file is no problem. However I get an error saying "Your file appears not to be a valid OLE2 document" as soon as I create a name for a new Excel file.

public void selectExcelFile() {

String excelFileName = null; // the name/directory/address of the excel file created/selected

FileInputStream excelFileIn = null; // allows us to connect to the Excel file so we can read it

FileOutputStream excelFileOut = null; // allows us to connect to the Excel file so we can write to it

ExcelFileUtility eUtil = new ExcelFileUtility(); // used open an excel file

if(columnsQuery != null) {

try {

excelFileName = eUtil.getFile(FileExtensions.XLS); // file extension = ".xls"

if(excelFileName != null) {

excelFileIn = new FileInputStream(new File(excelFileName));

workbook = new HSSFWorkbook(excelFileIn);

exportColsToWorkbook(columnsQuery);

excelFileOut = new FileOutputStream(excelFileName);

workbook.write(excelFileOut);

// close everything

workbook.close();

excelFileIn.close();

excelFileOut.close();

}

}

catch (IOException e) {

e.printStackTrace();

}

catch (SQLException e) {

e.printStackTrace();

}

}

}

And then:

public String getFile(String extension) {

String result = null;

if(extension != null) {

int choice = askIfNewFile();

if(choice == 0) { // yes, create new file

result = createFile(extension);

}

else { // no, select existing file

result = getFileLocation();

}

}

else {

System.out.println("No file extension.");

}

return result;

}

public String createFile(String extension) throws IOException {

String newFileName = "";

File newFile = null;

boolean isCreated = false;

JFrame frame = new JFrame("Creating a New ." + extension + " File");

String result = null;

String dir = getFileDirectory();

System.out.println("DIR: " + dir);

if(dir != null) {

while(newFileName.isEmpty() || newFileName == null) {

// Used WorkbookUtil.createSafeSheetName to validate file name

// Please replace if there is a better option

newFileName = WorkbookUtil.createSafeSheetName(JOptionPane.showInputDialog(frame, "Enter new ." + extension + " file name:"));

}

newFile = new File(dir + "\\" + newFileName + "." + extension);

System.out.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值