i have this java code which should create xls file but the file cannot be open. Excel says the format of the file is not valid. I added all librairie for generating xlsx file, am i missing something here. 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. Changing your file extension to .xls
should work
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.