标签:
private void export_button_ActionEvent(ActionEvent e) throws IOException, WriteException {
// TODO Auto-generated method stub
try{
fileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
int returnVal=fileChooser.showSaveDialog(fileChooser);//弹出保存窗体
String filePath=null;
if(returnVal==JFileChooser.APPROVE_OPTION)
{
filePath=fileChooser.getSelectedFile().getAbsolutePath()+".xls";//设定导出格式
System.out.println("filePath:"+filePath);
}
WritableWorkbook book=Workbook.createWorkbook(new File(filePath));
WritableSheet sheet= book.createSheet("SheetName", 0); //创建sheet
for(int col = 0;col<defaultTableModel.getColumnCount();col++){
try{
sheet.addCell(new Label(col,0,defaultTableModel.getColumnName(col))); //向excel中写入列标题
}catch(Exception e2){
e2.printStackTrace();
}
}
// 向excel写入数据
for (int j = 0; j < defaultTableModel.getColumnCount(); j++)
{
//System.out.println("列数:"+defaultTableModel.getColumnCount());
for (int i = 0; i < stocker_name.size(); i++)
{
//System.out.println("行数:"+defaultTableModel.getRowCount());
try{
sheet.addCell(new Label(j,i+1,defaultTableModel.getValueAt(i, j).toString())); //向excel中写入内容
System.out.println("第"+i+"行"+"第"+j+"列:"+defaultTableModel.getValueAt(i, j).toString());
}catch(Exception e1)
{
e1.printStackTrace();
}
}
}
book.write();
book.close();
//JOptionPane.showMessageDialog(null, "文件导出成功");
}catch(Exception e1){
e1.printStackTrace();
}
}
利用jxl.jar包
标签:
原文地址:http://www.cnblogs.com/Hblog/p/5502393.html