[英]Java creates excel file but i am not able to open the file
i have this java code which should create xls file but the file cannot be open.我有这个 java 代码,它应该创建 xls 文件,但文件无法打开。 Excel says the format of the file is not valid.
Excel 说文件格式无效。 I added all librairie for generating xlsx file, am i missing something here.
我添加了所有用于生成 xlsx 文件的库,我在这里遗漏了什么。 Any help please Thank you
任何帮助请谢谢
Here is the code:这是代码:
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import com.google.common.collect.Table.Cell;
import jxl.Workbook;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import java.util.concurrent.TimeUnit;
XSSFWorkbook workbook = new XSSFWorkbook();
XSSFSheet sheet = workbook.createSheet("Trade");
Map < String, Object[] > data = new TreeMap < String, Object[] > ();
data.put("1", new Object[] {
"Code"
});
Set < String > keyset = data.keySet();
int rownum = 0;
for (String key: keyset) {
Row row = sheet.createRow(rownum++);
Object[] objArr = data.get(key);
int cellnum = 0;
for (Object obj: objArr) {
org.apache.poi.ss.usermodel.Cell cell = row.createCell(cellnum++);
if (obj instanceof String)
cell.setCellValue((String) obj);
else if (obj instanceof Integer)
cell.setCellValue((Integer) obj);
}
}
try {
FileOutputStream out = new FileOutputStream(new
File("C:\\temp\\demo.xlsx"));
workbook.write(out);
out.close();
System.out.println("success");
} catch (Exception e) {
e.printStackTrace();
}
Apache POI is notably disagreeable with newer excel filetypes. Apache POI 明显不适合较新的 excel 文件类型。 Changing your file extension to
.xls
should work将文件扩展名更改为
.xls
应该可以
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.