您的位置:首页 > 其它

使用poi将jtable数据导出到excel中

2016-05-17 15:32 489 查看
所需要的jar包下载

org.apache.poi.hssf 是Horrible SpreadSheet

Format的缩写,通过HSSF,你可以用纯Java代码来读取、写入、修改Excel文件。HSSF

为读取操作提供了两类API:usermodel和eventusermodel,即“用户模型”和“事件-用户模型”。

package com.bohua.util;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.table.TableModel;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.Region;

public class JtableDownload {

JTable table;
FileOutputStream fos;
JFileChooser jfc = new JFileChooser();
private String dep;
private String year;
private String month;
private int count;
public static  boolean flag;

public JtableDownload(JTable table,String dep,String year,String month,int count) {
this.table = table;
this.year = year;
this.dep = dep;
this.month = month;
this.count = count;

if(dep==null||dep==""){
dep="全部部门";
}
jfc.setSelectedFile(new File(dep+ year+"-"+month+"考勤下井统计信息"+"(共"+count+"人)"));
int result= jfc.showSaveDialog(null);
File file = jfc.getSelectedFile();
try {
if (result == JFileChooser.APPROVE_OPTION) {
this.fos = new FileOutputStream(file + ".xls");
flag=true;
} else {
flag=false;
JOptionPane.showMessageDialog(null,"您取消了下载!");
}
} catch (FileNotFoundException ex) {
JOptionPane.showMessageDialog(null, "已存在文件名相同的文件,要替换请先关闭!");
}
}

public void export() {
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet hs = wb.createSheet();
TableModel tm = table.getModel();
int row = tm.getRowCount();
int cloumn = tm.getColumnCount();
//样式设计
HSSFCellStyle style = wb.createCellStyle();
style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 居中
HSSFFont font = wb.createFont();
font.setFontHeightInPoints((short) 11);
style.setFont(font);

for (int j = 0; j < cloumn; j++) {
hs.setColumnWidth( j, (1 * 4000));   //设置列宽
}

HSSFCellStyle style1 = wb.createCellStyle();
style1.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 表头居中
HSSFFont font1 = wb.createFont();
font1.setFontHeightInPoints((short) 15);
font1.setBoldweight((short) 700);
style1.setFont(font1);

HSSFRow hrtitle = hs.createRow(0);

HSSFRichTextString title = new HSSFRichTextString("考勤下井统计");
HSSFCell hctit = hrtitle.createCell(0);
hctit.setCellStyle(style1);
hctit.setCellValue(title);
Region regiontit = new Region(0, (short) 0, 0, (short) 3);
hs.addMergedRegion(regiontit);

if(dep==null||dep==""){
dep="全部部门";
}
HSSFRow hr0 = hs.createRow(1);

HSSFRichTextString srt0 = new HSSFRichTextString("部门名称:"+dep);
HSSFRichTextString srt1 = new HSSFRichTextString("时间:"+year+"年"+month+"月");
HSSFRichTextString srt2 = new HSSFRichTextString("总人数:"+count);
HSSFCell hc0 = hr0.createCell(1);
HSSFCell hc1 = hr0.createCell(3);
HSSFCell hc2 = hr0.createCell(5);
hc0.setCellStyle(style1);
hc1.setCellStyle(style1);
hc2.setCellStyle(style1);
hc0.setCellValue(srt0);
hc1.setCellValue(srt1);
hc2.setCellValue(srt2);
Region region0 = new Region(0, (short) 0, 0, (short) 2);
Region region1 = new Region(0, (short) 3, 0, (short) 4);
Region region2 = new Region(0, (short) 5, 0, (short) 8);
//参数1:行号 参数2:起始列号 参数3:行号 参数4:终止列号
hs.addMergedRegion(region0);
hs.addMergedRegion(region1);
hs.addMergedRegion(region2);

for (int i = 0; i < row + 1; i++) {
HSSFRow hr = hs.createRow(i+2);
for (int j = 0; j < cloumn; j++) {
if (i == 0) {
String value = tm.getColumnName(j);
HSSFRichTextString srts = new HSSFRichTextString(value);
HSSFCell hc = hr.createCell((short) j);
hc.setCellStyle(style1);
hc.setCellValue(srts);
} else {
if (tm.getValueAt(i - 1, j) != null) {
String value = tm.getValueAt(i - 1, j).toString();
HSSFRichTextString srts = new HSSFRichTextString(value);
HSSFCell hc = hr.createCell((short) j);
hc.setCellStyle(style);
if (value.equals("") || value == null) {
hc.setCellValue(new HSSFRichTextString(""));
} else {
hc.setCellValue(srts);
}
}
}
}
}

try {
wb.write(fos);
fos.close();
JOptionPane.showMessageDialog(null,"下载完成");
} catch (IOException ex) {

}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: