Java中将JTable中的数据导出到Excel中
来源:互联网 发布:贵阳大数据学校招生 编辑:程序博客网 时间:2024/05/18 02:09
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import javax.swing.JFileChooser;
import javax.swing.JTable;
import javax.swing.filechooser.FileFilter;
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.HSSFColor;
public class ExportExcel {
JTable table;
FileOutputStream fos;
JFileChooser jfc = new JFileChooser();
public ExportExcel(JTable table) {
this.table = table;
jfc.addChoosableFileFilter(new FileFilter() {
public boolean accept(File file) {
return (file.getName().indexOf("xls") != -1);
}
public String getDescription() {
return "Excel";
}
});
jfc.showSaveDialog(null);
File file = jfc.getSelectedFile();
try {
this.fos = new FileOutputStream(file + ".xls");
} catch (FileNotFoundException ex) {
ex.printStackTrace();
}
}
public void export() {
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet hs = wb.createSheet();
TableModel tm = table.getModel();
int row = tm.getRowCount();
int cloumn = tm.getColumnCount();
// System.out.println("row " + row + " column " + cloumn);
HSSFCellStyle style = wb.createCellStyle();
style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
style.setBorderRight(HSSFCellStyle.BORDER_THIN);
style.setBorderTop(HSSFCellStyle.BORDER_THIN);
style.setFillForegroundColor(HSSFColor.LIGHT_GREEN.index);
style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
HSSFFont font = wb.createFont();
font.setFontHeightInPoints((short) 11);
style.setFont(font);
HSSFCellStyle style1 = wb.createCellStyle();
style1.setBorderBottom(HSSFCellStyle.BORDER_THIN);
style1.setBorderLeft(HSSFCellStyle.BORDER_THIN);
style1.setBorderRight(HSSFCellStyle.BORDER_THIN);
style1.setBorderTop(HSSFCellStyle.BORDER_THIN);
style1.setFillForegroundColor(HSSFColor.ORANGE.index);
style1.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
HSSFFont font1 = wb.createFont();
font1.setFontHeightInPoints((short) 15);
font1.setBoldweight((short) 700);
style1.setFont(font);
for (int i = 0; i < row + 1; i++) {
HSSFRow hr = hs.createRow(i);
for (int j = 0; j < cloumn; j++) {
if (i == 0) {
String value = tm.getColumnName(j);
// System.out.println("value " + value);
int len = value.length();
hs.setColumnWidth((short) j, (short) (len * 400));
HSSFRichTextString srts = new HSSFRichTextString(value);
HSSFCell hc = hr.createCell((short) j);
hc.setEncoding((short) 1);
hc.setCellStyle(style1);
hc.setCellValue(srts);
} else {
System.out.println("vlue " + tm.getValueAt(i - 1, j));
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.setEncoding((short) 1);
hc.setCellStyle(style);
if (value.equals("") || value == null) {
hc.setCellValue(new HSSFRichTextString(""));
} else {
hc.setCellValue(srts);
}
}
}
}
}
try {
wb.write(fos);
fos.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import javax.swing.JFileChooser;
import javax.swing.JTable;
import javax.swing.filechooser.FileFilter;
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.HSSFColor;
public class ExportExcel {
JTable table;
FileOutputStream fos;
JFileChooser jfc = new JFileChooser();
public ExportExcel(JTable table) {
this.table = table;
jfc.addChoosableFileFilter(new FileFilter() {
public boolean accept(File file) {
return (file.getName().indexOf("xls") != -1);
}
public String getDescription() {
return "Excel";
}
});
jfc.showSaveDialog(null);
File file = jfc.getSelectedFile();
try {
this.fos = new FileOutputStream(file + ".xls");
} catch (FileNotFoundException ex) {
ex.printStackTrace();
}
}
public void export() {
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet hs = wb.createSheet();
TableModel tm = table.getModel();
int row = tm.getRowCount();
int cloumn = tm.getColumnCount();
// System.out.println("row " + row + " column " + cloumn);
HSSFCellStyle style = wb.createCellStyle();
style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
style.setBorderRight(HSSFCellStyle.BORDER_THIN);
style.setBorderTop(HSSFCellStyle.BORDER_THIN);
style.setFillForegroundColor(HSSFColor.LIGHT_GREEN.index);
style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
HSSFFont font = wb.createFont();
font.setFontHeightInPoints((short) 11);
style.setFont(font);
HSSFCellStyle style1 = wb.createCellStyle();
style1.setBorderBottom(HSSFCellStyle.BORDER_THIN);
style1.setBorderLeft(HSSFCellStyle.BORDER_THIN);
style1.setBorderRight(HSSFCellStyle.BORDER_THIN);
style1.setBorderTop(HSSFCellStyle.BORDER_THIN);
style1.setFillForegroundColor(HSSFColor.ORANGE.index);
style1.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
HSSFFont font1 = wb.createFont();
font1.setFontHeightInPoints((short) 15);
font1.setBoldweight((short) 700);
style1.setFont(font);
for (int i = 0; i < row + 1; i++) {
HSSFRow hr = hs.createRow(i);
for (int j = 0; j < cloumn; j++) {
if (i == 0) {
String value = tm.getColumnName(j);
// System.out.println("value " + value);
int len = value.length();
hs.setColumnWidth((short) j, (short) (len * 400));
HSSFRichTextString srts = new HSSFRichTextString(value);
HSSFCell hc = hr.createCell((short) j);
hc.setEncoding((short) 1);
hc.setCellStyle(style1);
hc.setCellValue(srts);
} else {
System.out.println("vlue " + tm.getValueAt(i - 1, j));
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.setEncoding((short) 1);
hc.setCellStyle(style);
if (value.equals("") || value == null) {
hc.setCellValue(new HSSFRichTextString(""));
} else {
hc.setCellValue(srts);
}
}
}
}
}
try {
wb.write(fos);
fos.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
//方法的調用
ExportExcel exportExcel = new ExportExcel(this.jTable1);
exportExcel.export();
转载自: http://blog.csdn.net/crl_lxy/article/details/2552140
- Java中将JTable中的数据导出到Excel中
- java中将数据和图片导出到Excel文件中
- Java中将数据导出到Excel
- 使用poi将jtable数据导出到excel中
- java swing 导出Jtable里面的数据到excel
- struts2中将jsp table中的数据导出到excel表格中
- 在aspx页面中将DataTable中的数据通过GridView导出到Excel中
- c#中将数据库数据导出到EXCEL中
- vb中将数据导出到excel表格中
- VC中将数据导出到Excel表格中
- C#中将ListView中数据导出到Excel代码
- jtable导出到excel
- netbeans中将表格中的数据写到Excel中
- Gridview中的数据导出到excel中
- java从数据库中将数据导入到excel中
- 将数据库中的数据导出到Excel中(直接导出到磁盘,Java方式)
- java中jxl导出数据到excel
- Java中数据导出到Excel实例
- OpenMP Tutorial学习笔记(12)OpenMP运行库函数(Run-Time Library Routines)
- 12款高质量的免费 HTML 网页模板下载
- Android开发入门之路(续篇)
- mysql变量使用总结
- iphone4 电话截获
- Java中将JTable中的数据导出到Excel中
- gvim字体设置、背景设置、保存为默认状态
- 找出数组中第k大小的数,输出数所在的位置
- oenSession()和 getCurrentSession()的区别
- BREW怎样支持全局变量和静态变量
- android makefile(android.mk)分析(序)
- D3DXCreateTextureFromFileEx中参数介绍
- qt4环境变量
- qt4程序执行流程