[英]saving files JExcel with Filechooser
我正在嘗試使用jxl保存excel文件,但是當我嘗試保存文件時,必須重新鍵入並保存無內容的純文本。 我正在使用一個類來生成Excel文件。
public class lsheet {
public void fillData(JTable table, File file){
try{
WritableWorkbook workbook1 = Workbook.createWorkbook(file);
WritableSheet sheet1 = workbook1.createSheet("Data 1", 0);
TableModel model = table.getModel();
for (int i = 0; i < model.getColumnCount(); i++) {
Label column = new Label(i, 0, model.getColumnName(i));
sheet1.addCell(column);
}
int j = 0;
for (int i = 0; i < model.getRowCount(); i++) {
for (j = 0; j < model.getColumnCount(); j++) {
Label row = new Label(j, i + 1,
model.getValueAt(i, j).toString());
sheet1.addCell(row);
}
}
workbook1.write();
workbook1.close();
}catch(Exception ex){
ex.printStackTrace();
}
}
}
這個操作按鈕,如何在表中顯示帶有已加載數據的文件選擇器?
lsheet sht = new lshtCalc();
java.util.Date today = new java.util.Date();
SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd");
try{
String name="";
JFileChooser file=new JFileChooser();
file.showSaveDialog(this);
File save =file.getSelectedFile();
if(save !=null){
FileWriter save = new FileWriter(save + ".xls");
sht.fillData(tblCliente, new File("Report-Client-"+ sdf2.format(today).toString()+".xls"));
save.close();
}
}catch(Exception ex){
ex.printStackTrace();
}
您傳遞給fillData方法的文件對象是使用時間戳創建的,因此它將使用JTable中的內容在工作區中創建一個具有該名稱的文件。
使用JFileChooser選擇的文件僅用於創建文件編寫器並關閉它。 因此,它將在您選擇的位置創建一個空文件。
嘗試此更改:
JFileChooser file=new JFileChooser();
file.showSaveDialog(this);
File save =file.getSelectedFile();
if(save !=null){
sht.fillData(tblCliente, save);
}
}catch(Exception ex){
ex.printStackTrace();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.