Reputation: 502
Hi I want to create a excel file, I need to add data from java application and need to view those data again using java application.I used java jxl library files to do it. I can open excel file created with MS excel it open without any problem.But I cant open excel files created by my java app.Because I think the reason is my app not created excel file.It just create file with ".xls" as a extension.I can open file in excel anyway.
When I open file created by my app, using ms excel error occured " The file format and extension not match. and blah blah"
But ms excel open that file anyway.
when i'm going to save it it says "Some features in you work book might lost if you save it as text tab delimited"
How to solve this
code to create xls file.Please help me to complete this code
try {
WritableWorkbook copy=Workbook.createWorkbook(new File("C:\\Users\\FxMax\\Desktop\\demo.xls"));//create file
WritableSheet Wsheet = copy.getSheet(0); //create sheet
TableModel mode=table.getModel(); //create table model for jtable
String s=mode.getValueAt(0, 0).toString();//get value at 0,0 cell @jtable
copy.write();
copy.close();
} catch (Exception e) {
}
this is the code use to read file
JFileChooser choose = new JFileChooser();
choose.setFileSelectionMode(JFileChooser.FILES_ONLY);
FileNameExtensionFilter fileF = new FileNameExtensionFilter("Excel file", "xls");
choose.setFileFilter(fileF);
int res = choose.showOpenDialog(jFileChooser1);
if (res == JFileChooser.APPROVE_OPTION) {
//File file =choose.getSelectedFile(); //getFile
String filePath = choose.getSelectedFile().getAbsolutePath();//getFilePath
File f = new File(filePath);
//System.out.println(filePath);
Workbook w;
try {
w = Workbook.getWorkbook(f);
Sheet sheet = w.getSheet(0);
for (int i = 1; i < sheet.getRows(); i++) {
String cell0 = sheet.getCell(0, i).getContents();
String cell1 = sheet.getCell(1, i).getContents();
String f1 = sheet.getCell(2, i).getContents();
float cell2 = Float.valueOf(f1);
float cell3 = Float.valueOf(sheet.getCell(3, i).getContents());
Object[] cells = {cell0, cell1, cell2, cell3,};
DefaultTableModel model = (DefaultTableModel) jTable1.getModel();
model.addRow(cells);
}
} catch (Exception e) {
}
} else {
}
}
Upvotes: 0
Views: 192
Reputation: 11234
If you use poi
it is easier.
Add poi dependency to your pom
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.10-FINAL</version>
</dependency>
And use it like this:
...
...
int rowNum = 0;
int colNum = 0;
XSSFWorkbook workbook = new XSSFWorkbook();
XSSFSheet sheet = workbook.createSheet("sheetname");
Row row = sheet.createRow(rowNum++);
row.createCell(colNum++).setCellValue("your value");
...
...
FileOutputStream fileOut = new FileOutputStream("your excel (xlsx) file name");
workbook.write(fileOut);
fileOut.close;
Reading is similar. Check poi
documentation.
Upvotes: 2
Reputation: 81529
Apache POI worked like this for me:
HSSFWorkbook workbook = new HSSFWorkbook();
Iterator<E> iterator = elementList.iterator();
for (int wsNum = 0; wsNum <= (totalNumberOfElements / 65536); wsNum++) {
HSSFSheet worksheet = workbook.createSheet("Name of worksheet " + wsNum);
for (long i = wsNum * 65536; i < ((wsNum + 1) * 65536); i++) {
int ii = ((Long) (i % 65536)).intValue();
if (!iterator.hasNext()) {
break;
}
HSSFRow row = worksheet.createRow(ii);
E e = iterator.next();
for(int columnNum = 0; columnNum < maxColumnNum; columnNum++)
{
HSSFCell cell = row.createCell(columnNum);
cell.setCellValue(e.getProperty(columnNum)); //figure out something smart for this one
}
}
}
for(int columnNum = 0; columnNum < maxColumnNum; columnNum++)
worksheet.autoSizeColumn(columnNum);
}
return workbook;
and
workbook.write(response.getOutputStream()); //it needs a Stream so that can be a FileOutputStream
The important thing to note is that if you want to have a lot of data exported into the Excel file, due to the limitations of the original XLS format you can only have a total of 65536 rows in a work sheet.
Upvotes: 1