在Java编程中怎么将从数据库查询出来的数据导成Excel文件?
- 提问者网友:蔚蓝的太阳
- 2021-03-14 23:19
- 五星知识达人网友:北城痞子
- 2021-03-15 00:38
- 1楼网友:長槍戰八方
- 2021-03-15 01:32
import jxl.*; import jxl.write.*; import java.io.*; import java.io.file.*; import java.util.*; public class excel { public static void main(string[] args) { string targetfile = "c:/out.xls";//输出的excel文件名 string worksheet = "list";//输出的excel文件工作表名 string[] title = {"id","name","describ"};//excel工作表的标题 writableworkbook workbook; try { //创建可写入的excel工作薄,运行生成的文件在tomcat/bin下 //workbook = workbook.createworkbook(new file("output.xls")); system.out.println("begin"); outputstream os=new fileoutputstream(targetfile); workbook=workbook.createworkbook(os); writablesheet sheet = workbook.createsheet(worksheet, 0); //添加第一个工作表 //writablesheet sheet1 = workbook.createsheet("mysheet1", 1); //可添加第二个工作 jxl.write.label label; for (int i=0; i<title.length; i++) { //label(列号,行号 ,内容 ) label = new jxl.write.label(i, 0, title[i]); //put the title in row1 sheet.addcell(label); } //下列添加的对字体等的设置均调试通过,可作参考用 //添加数字 jxl.write.number number = new jxl.write.number(3, 4, 3.14159); //put the number 3.14159 in cell d5 sheet.addcell(number); //添加带有字型formatting的对象 jxl.write.writablefont wf = new jxl.write.writablefont(writablefont.times,10,writablefont.bold,true); jxl.write.writablecellformat wcff = new jxl.write.writablecellformat(wf); jxl.write.label labelcf = new jxl.write.label(4,4,"文本",wcff); sheet.addcell(labelcf); //添加带有字体颜色,带背景颜色 formatting的对象 jxl.write.writablefont wfc = new jxl.write.writablefont(writablefont.arial,10,writablefont.bold,false,jxl.format.underlinestyle.no_underline,jxl.format.colour.red); jxl.write.writablecellformat wcffc = new jxl.write.writablecellformat(wfc); wcffc.setbackground(jxl.format.colour.blue); jxl.write.label labelcfc = new jxl.write.label(1,5,"带颜色",wcffc); sheet.addcell(labelcfc); //添加带有formatting的number对象 jxl.write.numberformat nf = new jxl.write.numberformat("#.##"); jxl.write.writablecellformat wcfn = new jxl.write.writablecellformat(nf); jxl.write.number labelnf = new jxl.write.number(1,1,3.1415926,wcfn); sheet.addcell(labelnf); //3.添加boolean对象 jxl.write.boolean labelb = new jxl.write.boolean(0,2,false); sheet.addcell(labelb); //4.添加datetime对象 jxl.write.datetime labeldt = new jxl.write.datetime(0,3,new java.util.date()); sheet.addcell(labeldt); //添加带有formatting的dateformat对象 jxl.write.dateformat df = new jxl.write.dateformat("ddmmyyyyhh:mm:ss"); jxl.write.writablecellformat wcfdf = new jxl.write.writablecellformat(df); jxl.write.datetime labeldtf = new jxl.write.datetime(1,3,new java.util.date(),wcfdf); sheet.addcell(labeldtf); //和宾单元格 //sheet.mergecells(int col1,int row1,int col2,int row2);//左上角到右下角 sheet.mergecells(4,5,8,10);//左上角到右下角 wfc = new jxl.write.writablefont(writablefont.arial,40,writablefont.bold,false,jxl.format.underlinestyle.no_underline,jxl.format.colour.green); jxl.write.writablecellformat wchb = new jxl.write.writablecellformat(wfc); wchb.setalignment(jxl.format.alignment.centre); labelcfc = new jxl.write.label(4,5,"单元合并",wchb); sheet.addcell(labelcfc); // //设置边框 jxl.write.writablecellformat wcsb = new jxl.write.writablecellformat(); wcsb.setborder(jxl.format.border.all,jxl.format.borderlinestyle.thick); labelcfc = new jxl.write.label(0,6,"边框设置",wcsb); sheet.addcell(labelcfc); workbook.write(); workbook.close(); }catch(exception e) { e.printstacktrace(); } system.out.println("end"); runtime r=runtime.getruntime(); process p=null; //string cmd[]={"notepad","exec.java"}; string cmd[]={"c:\\program files\\microsoft office\\office\\excel.exe","out.xls"}; try{ p=r.exec(cmd); } catch(exception e){ system.out.println("error executing: "+cmd[0]); } } }