java 数据库数据导出到EXCEL

来源:互联网 发布:济南行知小学地址 编辑:程序博客网 时间:2024/06/05 08:22

大家可以自己写几个表测试下。本文用了两个表,一个是student另外一个是paper。CSDN的格式和java的不太一样所以可读性有点差。用的jar包有 jxl.jar itcast-tools.jar
源码 excelDao

package jwfz.excel.dao;import java.io.FileOutputStream;import java.io.OutputStream;import java.sql.SQLException;import java.text.SimpleDateFormat;import java.util.ArrayList;import java.util.Date;import java.util.List;import org.apache.commons.dbutils.handlers.BeanListHandler;import cn.itcast.jdbc.TxQueryRunner;import jwfz.excel.domain.excelDomain;import jxl.Workbook;import jxl.format.Alignment;import jxl.format.Border;import jxl.format.BorderLineStyle;import jxl.format.CellFormat;import jxl.format.Colour;import jxl.write.Label;import jxl.write.WritableCellFormat;import jxl.write.WritableFont;import jxl.write.WritableSheet;import jxl.write.WritableWorkbook;import jxl.write.WriteException;public class excelDao {    static TxQueryRunner qr=new TxQueryRunner();    //获取该状态下的学生论文信息,然后导出    public static List<excelDomain> getLunwenInfo(String statu) throws SQLException{        String sql="select student.StuNum,stuName,stuclass,authorMajor,stuPhonenum , paperName,"            +"paper.column2, authorName, authorId, authorMajor, otherAuthors ,journalName ,journalCN , periodicalSituation , releaseTime "            +"from student  inner join paper on student.StuNum=paper.stunum where shenhestatu=?";        return qr.query(sql, new BeanListHandler<excelDomain>(excelDomain.class),statu);    }    public static void main(String[] args) throws SQLException {        List<excelDomain> excel=new ArrayList<excelDomain>();        excel=getLunwenInfo("No");        for(int i=0;i<excel.size();i++){            System.out.println(excel.get(i).getPaperName());            }        writeExcel( excel);    }    /**     * Excel数据     */    public static void writeExcel(List<excelDomain> excel){           String[] title = {"序号","学号","登记日期","论文名称","第一作者姓名","第一作者学号","第一作者专业","手机号码","    其他作者","发表期刊名称","期刊CN号","论文期刊情况","论文刊出时间 "};                   try {                       // 获得开始时间       long start = System.currentTimeMillis();               // 输出的excel的路径       String filePath = "d:\\Excel.xls";          // 创建Excel工作薄  WritableWorkbook wwb;        // 新建立一个jxl文件,即在d盘下生成testJXL.xls    OutputStream os = new FileOutputStream(filePath);   wwb=Workbook.createWorkbook(os);    // 添加第一个工作表并设置第一个Sheet的名字   WritableSheet sheet = wwb.createSheet("学生论文信息", 0);       Label label;       for(int i=0;i<title.length;i++){        // Label(x,y,z) 代表单元格的第x+1列,第y+1行, 内容z       // 在Label对象的子对象中指明单元格的位置和内容       label = new Label(i,0,title[i]);    label = new Label(i, 0, title[i], getHeader());    // 将定义好的单元格添加到工作表中      sheet.addCell(label);           }           // 下面是填充数据              /*               * 保存数字到单元格,需要使用jxl.write.Number             * 必须使用其完整路径,否则会出现错误            * */         for(int i=0;i<excel.size();i++){                 // 填充excel 第一列                label = new Label(0,i+1,i+1+""); //序号列               sheet.addCell(label);    // 填充excel 第2列    label = new Label(1,i+1,excel.get(i).getStuNum());//学号       sheet.addCell(label);         // 填充excel 第3列 label = new Label(2,i+1,excel.get(i).getColumn2());  //论文登记时间                         sheet.addCell(label);                      // 填充excel 第4列label = new Label(3,i+1,excel.get(i).getPaperName());  //论文名称                         sheet.addCell(label);             // 填充excel 第5列 label = new Label(4,i+1,excel.get(i).getAuthorName());  //第一作业 sheet.addCell(label);    // 填充excel 第6列label = new Label(5,i+1,excel.get(i).getAuthorId());  //获取第一作业学号    sheet.addCell(label);              // 填充excel 第7列      label = new Label(6,i+1,excel.get(i).getAuthorMajor());  //第一作者专业            sheet.addCell(label);                         // 填充excel 第8列label = new Label(6,i+1,excel.get(i).getPhoneNumber());  //手机号码                         sheet.addCell(label);                     // 填充excel 第9列  label = new Label(7,i+1,excel.get(i).getOtherAuthors());  //其他作者                         sheet.addCell(label);                      // 填充excel 第10列 label = new Label(8,i+1,excel.get(i).getJournalName());  //发表期刊名称                         sheet.addCell(label);                      // 填充excel 第11列  label = new Label(8,i+1,excel.get(i).getJournalCN());  //发表期刊CN号                         sheet.addCell(label);                      // 填充excel 第12列 label = new Label(9,i+1,excel.get(i).getPeriodicalSituation()); //论文期刊情况                          sheet.addCell(label);                      // 填充excel 第13列label = new Label(10,i+1,excel.get(i).getReleaseTime()); //论文刊出时间                          sheet.addCell(label);                     }                     /*                       *                         * 定义公共字体格式                       * 通过获取一个字体的样式来作为模板                      * 首先通过web.getSheet(0)获得第一个sheet                      * 然后取得第一个sheet的第二列,第一行也就是"产品名称"的字体                       * */                     CellFormat cf = wwb.getSheet(0).getCell(1, 0).getCellFormat();                       WritableCellFormat wc = new WritableCellFormat();                       // 设置居中                        wc.setAlignment(Alignment.CENTRE);                        // 设置边框线                      wc.setBorder(Border.ALL, BorderLineStyle.THIN);                     // 写入数据                      wwb.write();                       // 关闭文件                       wwb.close();                      long end = System.currentTimeMillis();                       System.out.println("----完成该操作共用的时间是:"+(end-start)/1000);                   } catch (Exception e) {                       System.out.println("---出现异常---");                        e.printStackTrace();                  }           }    public static WritableCellFormat getHeader(){           WritableFont font = new  WritableFont(WritableFont.TIMES, 10 ,WritableFont.BOLD);//定义字体           try {            font.setColour(Colour.BLUE);//蓝色字体           } catch (WriteException e1) {            // TODO 自动生成 catch 块            e1.printStackTrace();           }           WritableCellFormat format = new  WritableCellFormat(font);           try {            format.setAlignment(jxl.format.Alignment.CENTRE);//左右居中            format.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);//上下居中            format.setBorder(Border.ALL,BorderLineStyle.THIN,Colour.BLACK);//黑色边框            format.setBackground(Colour.YELLOW);//黄色背景           } catch (WriteException e) {            // TODO 自动生成 catch 块            e.printStackTrace();           }           return format;          }}
0 0
原创粉丝点击