怎样将JTable中的数据导出成一个excel文件?(非jsp)有个“导出成excel”按钮
展开全部
需要POI jar包。一下是我写的一段核心代码
public static HSSFWorkbook getOrderWorkbook(String templatePath, List<Order> dataList) {
InputStream stream = null;
try {
stream = new FileInputStream(templatePath);
POIFSFileSystem fileSystem = new POIFSFileSystem(stream);
HSSFWorkbook workbook = new HSSFWorkbook(fileSystem);
HSSFSheet sheet = workbook.getSheetAt(0);
int rowIndex = 1;
for(int i=0;i<dataList.size();i++,rowIndex++){
Order order = dataList.get(i);
HSSFRow row = sheet.createRow(rowIndex);
row.createCell(0).setCellValue(new HSSFRichTextString(order.getOrderNo()));
row.createCell(1).setCellValue(new HSSFRichTextString(order.getGoodsName()));
row.createCell(2).setCellValue(order.getTotalMoney()/100);
row.createCell(3).setCellValue(order.getUnitPrice()/100);
row.createCell(4).setCellValue(order.getGoodsSum());
row.createCell(5).setCellValue(new HSSFRichTextString(order.getOrderTimeStr()));
row.createCell(6).setCellValue(new HSSFRichTextString(order.getPayState() == 2 ? "已付款" : "未付款"));
row.createCell(7).setCellValue(new HSSFRichTextString(order.getNote()));
row.createCell(8).setCellValue(new HSSFRichTextString(order.getNote2()));
row.createCell(9).setCellValue(new HSSFRichTextString(order.getInsertUser()));
row.createCell(10).setCellValue(new HSSFRichTextString(order.getTraderName()));
}
return workbook;
} catch (Exception e) {
e.printStackTrace();
}finally{
try {
if(stream != null){
stream.close();
stream = null;
}
} catch (Exception e) {
e.printStackTrace();
}
}
return null;
}
templatePath:模板地址,定义excel格式。
dataList:数据集合
public static HSSFWorkbook getOrderWorkbook(String templatePath, List<Order> dataList) {
InputStream stream = null;
try {
stream = new FileInputStream(templatePath);
POIFSFileSystem fileSystem = new POIFSFileSystem(stream);
HSSFWorkbook workbook = new HSSFWorkbook(fileSystem);
HSSFSheet sheet = workbook.getSheetAt(0);
int rowIndex = 1;
for(int i=0;i<dataList.size();i++,rowIndex++){
Order order = dataList.get(i);
HSSFRow row = sheet.createRow(rowIndex);
row.createCell(0).setCellValue(new HSSFRichTextString(order.getOrderNo()));
row.createCell(1).setCellValue(new HSSFRichTextString(order.getGoodsName()));
row.createCell(2).setCellValue(order.getTotalMoney()/100);
row.createCell(3).setCellValue(order.getUnitPrice()/100);
row.createCell(4).setCellValue(order.getGoodsSum());
row.createCell(5).setCellValue(new HSSFRichTextString(order.getOrderTimeStr()));
row.createCell(6).setCellValue(new HSSFRichTextString(order.getPayState() == 2 ? "已付款" : "未付款"));
row.createCell(7).setCellValue(new HSSFRichTextString(order.getNote()));
row.createCell(8).setCellValue(new HSSFRichTextString(order.getNote2()));
row.createCell(9).setCellValue(new HSSFRichTextString(order.getInsertUser()));
row.createCell(10).setCellValue(new HSSFRichTextString(order.getTraderName()));
}
return workbook;
} catch (Exception e) {
e.printStackTrace();
}finally{
try {
if(stream != null){
stream.close();
stream = null;
}
} catch (Exception e) {
e.printStackTrace();
}
}
return null;
}
templatePath:模板地址,定义excel格式。
dataList:数据集合
追问
大侠你好。我的JTable里面用的是DefaultTableModel:JTable table = new JTable(model);在这里model要怎样跟您那个dataList联系起来呢?
追答
你把每一行数据做为一个对象封装起来,有多行的话就是一个集合了。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询