我尝试使用HSSFWorkbook将Jtable数据导出到Excel表格中。我从JTable获取所有数据,但十进制格式与JTable不同。请任何人帮助我......
例如:
JTable - > 4555个
导出的xls - > 4,55479080958
这里用于获取JTable内容的命令:
try {
for (int i = 0; i < tableModel.getRowCount(); i++) {
fRow1 = workbookSheet.createRow((short) i);
for (int j = 0; j < tableModel.getColumnCount(); j++) {
HSSFCell cell = fRow1.createCell((short) j);
cell.setCellValue(tableModel.getValueAt(i, j).toString());
}
}
FileOutputStream fileOutputStream;
fileOutputStream = new FileOutputStream("C:\\file.xls");
BufferedOutputStream bos = new BufferedOutputStream(fileOutputStream);
hssfWorkbook.write(bos);
bos.close();
fileOutputStream.close();
} catch (Exception e) {
for (int i = 0; i < tableModel.getColumnCount(); i++) {
fRow1 = workbookSheet.createRow((short) i);
for (int j = 0; j < tableModel.getColumnCount(); j++) {
HSSFCell cell = fRow1.createCell((short) j);
cell.setCellValue(tableModel.getValueAt(i, j).toString());
System.out.println(tableModel.getColumnName(j));
}
}
}
这里是JTable的代码:
public class FileModel extends AbstractTableModel implements FilenameFilter {
List<Object[]> data = new ArrayList<>();
String titles[] = new String[] { "File Name", "Pages", "Media Box Height", "Media Box Width", "Trim Box Height",
"Trim Box Width", "Path" };
Class<?> types[] = new Class[] { String.class, Integer.class, Float.class, Float.class, Float.class, Float.class,
String.class };
public FileModel() {
this("C:\\");
}
public FileModel(String directory) {
File inputFiles = new File(directory);
setFileStats(inputFiles);
}
// Implement the methods of the TableModel interface Only getRowCount(), getColumnCount() and getValueAt() are
// required.
//
@Override
public int getRowCount() {
return this.data.size();
}
@Override
public int getColumnCount() {
return this.titles.length;
}
@Override
public String getColumnName(int column) {
return this.titles[column];
}
@Override
public Class<?> getColumnClass(int column) {
return this.types[column];
}
@Override
public Object getValueAt(int row, int column) {
return this.data.get(row)[column];
}
答案 0 :(得分:0)
我使用了DecimalFormat:
DecimalFormat formatDecimal = new DecimalFormat("###.000");
稍后我使用DecimalFormat将Object添加到AbstractTable:
this.data.add(new Object[] { file.getName(), formatDecimal.format(123.42345),file.getAbsolutePath() });