这个我做过,不过我的思路和你的有点不一样.我的是这样的:
首先通过查询语句得到满足查询条件的数据,把这些数据写到一个JTable中,然后在把这个JTable的内容生成Excel文件.这样子用户可以在生成Excel文件前得到自己想要的文件.其实这个不难,我给你一些源代码,你可以自己看看,有不懂的可以再问.
/*
* TestExcel.java
*
* Created on 2004年8月3日, 下午2:15
*/package Autosale;
import java.io.File;import jxl.Workbook;
import jxl.write.WritableWorkbook;
import jxl.write.WritableSheet;
import jxl.write.WritableFont;
import jxl.write.WritableCellFormat;
import jxl.format.UnderlineStyle;
import jxl.write.Label;
import jxl.write.NumberFormat;
import jxl.write.DateFormat;public class TestExcel {
private final String FILE_NAME="c:\\test.xls";
private String []title={"Test1","Test2"};
private String [][]content = {{"1111","2222"},{"3333","4444"},{"5555","6666"}};
private File output;
private WritableWorkbook wbook;
private WritableSheet wsheet;
public TestExcel() {
init();
}
private void init() {
output=new File(FILE_NAME);
try{
wbook=Workbook.createWorkbook(output);
wsheet=wbook.createSheet("TEST EXCEL",0);
}
catch(Exception e){System.out.println(e.getMessage());}
}
public void export() {
try{
Label l=null;
jxl.write.Number n=null;
jxl.write.DateTime d=null;
WritableFont headerFont = new WritableFont(WritableFont.ARIAL, 12, WritableFont.BOLD, false, jxl.format.UnderlineStyle.NO_UNDERLINE, jxl.format.Colour.BLUE);
WritableCellFormat headerFormat = new WritableCellFormat(headerFont);
WritableFont titleFont = new WritableFont(WritableFont.ARIAL, 10, WritableFont.NO_BOLD, false, jxl.format.UnderlineStyle.NO_UNDERLINE, jxl.format.Colour.RED);
WritableCellFormat titleFormat = new WritableCellFormat(titleFont);
WritableFont detFont = new WritableFont(WritableFont.ARIAL, 10, WritableFont.NO_BOLD, false, jxl.format.UnderlineStyle.NO_UNDERLINE, jxl.format.Colour.BLACK);
WritableCellFormat detFormat = new WritableCellFormat(detFont);
l=new Label(0, 0, "TEST EXCEL", headerFormat);
wsheet.addCell(l);
//add cell
for(int i=0;i<title.length;i++) {
l=new Label(i, 1, title[i], titleFormat);
wsheet.addCell(l);
}
for(int j = 0; j<content[1].length; j ++){
for(int i = 0; i<=content[2].length; i ++) {
l=new Label(j,i+2,content[i][j],detFormat);
wsheet.addCell(l);
}}
wbook.write();
wbook.close();
}catch(Exception e){ e.printStackTrace(); }
}
public static void main(String []args) {
TestExcel test = new TestExcel();
test.export();
}
}
首先通过查询语句得到满足查询条件的数据,把这些数据写到一个JTable中,然后在把这个JTable的内容生成Excel文件.这样子用户可以在生成Excel文件前得到自己想要的文件.其实这个不难,我给你一些源代码,你可以自己看看,有不懂的可以再问.
/*
* TestExcel.java
*
* Created on 2004年8月3日, 下午2:15
*/package Autosale;
import java.io.File;import jxl.Workbook;
import jxl.write.WritableWorkbook;
import jxl.write.WritableSheet;
import jxl.write.WritableFont;
import jxl.write.WritableCellFormat;
import jxl.format.UnderlineStyle;
import jxl.write.Label;
import jxl.write.NumberFormat;
import jxl.write.DateFormat;public class TestExcel {
private final String FILE_NAME="c:\\test.xls";
private String []title={"Test1","Test2"};
private String [][]content = {{"1111","2222"},{"3333","4444"},{"5555","6666"}};
private File output;
private WritableWorkbook wbook;
private WritableSheet wsheet;
public TestExcel() {
init();
}
private void init() {
output=new File(FILE_NAME);
try{
wbook=Workbook.createWorkbook(output);
wsheet=wbook.createSheet("TEST EXCEL",0);
}
catch(Exception e){System.out.println(e.getMessage());}
}
public void export() {
try{
Label l=null;
jxl.write.Number n=null;
jxl.write.DateTime d=null;
WritableFont headerFont = new WritableFont(WritableFont.ARIAL, 12, WritableFont.BOLD, false, jxl.format.UnderlineStyle.NO_UNDERLINE, jxl.format.Colour.BLUE);
WritableCellFormat headerFormat = new WritableCellFormat(headerFont);
WritableFont titleFont = new WritableFont(WritableFont.ARIAL, 10, WritableFont.NO_BOLD, false, jxl.format.UnderlineStyle.NO_UNDERLINE, jxl.format.Colour.RED);
WritableCellFormat titleFormat = new WritableCellFormat(titleFont);
WritableFont detFont = new WritableFont(WritableFont.ARIAL, 10, WritableFont.NO_BOLD, false, jxl.format.UnderlineStyle.NO_UNDERLINE, jxl.format.Colour.BLACK);
WritableCellFormat detFormat = new WritableCellFormat(detFont);
l=new Label(0, 0, "TEST EXCEL", headerFormat);
wsheet.addCell(l);
//add cell
for(int i=0;i<title.length;i++) {
l=new Label(i, 1, title[i], titleFormat);
wsheet.addCell(l);
}
for(int j = 0; j<content[1].length; j ++){
for(int i = 0; i<=content[2].length; i ++) {
l=new Label(j,i+2,content[i][j],detFormat);
wsheet.addCell(l);
}}
wbook.write();
wbook.close();
}catch(Exception e){ e.printStackTrace(); }
}
public static void main(String []args) {
TestExcel test = new TestExcel();
test.export();
}
}
解决方案 »
- 【【sql同、prepareStatement怎么比createStatement慢了几百倍??】】
- 危如朝露,在线等!!!!!
- 怎么判断SQL中的Connection对象已经断连?
- JTextArea对象中用于清除所有内容的方法是什么啊
- scjp求助,这道题应该选什么,以及原因,谢谢
- FileOutputStream的疑问
- 请问迭代器
- (==★★★100分★★★==)Runtime.getRuntime().exec()的问题
- 淘宝API初学者
- 用java正则表达式(或其他简单方法)判断字符串是否含有除特定字母外的字符
- facon server是?
- 请教,为什么PDF在IE里不显示????奇怪问题
http://www.andykhan.com/jexcelapi/
祝你好运
我的是174605008
呵呵
你加我试试看:5609295
注明csdn,5257007
谢谢