【JAVA技術】導出爲excel代碼

  

try{

     OutputStream os = response.getOutputStream();

     response.reset();// 清空輸出流

     String filename = "查詢結果.xls"; //文件名

     response.setCharacterEncoding("utf-8");

   response.setHeader("Content-disposition","attachment;filename=\""
     + new String(name.getBytes("gb2312"),"ISO8859-1") + "\"");// 設定輸出文件頭 

     response.setContentType("application/msexcel");// 定義輸出類型

     WritableWorkbook wwb= Workbook.createWorkbook(os);//建立excel文件

     WritableFont font1= new WritableFont(WritableFont.TIMES,12,WritableFont.BOLD);

     WritableCellFormat format1=new WritableCellFormat(font1);

     format1.setAlignment(jxl.format.Alignment.CENTRE);

     format1.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);

     WritableSheet ws= wwb.createSheet("查詢結果",10); //創建表單

ws.addCell(new jxl.write.Label(j, i, "內容",format1));//往第i行、第j列添加內容爲“內容”,格式爲format1

     ws.setRowView(0,300);//指定行的寬度,可省

     ws.setColumnView(1,25);//指定列的寬度,可省

     ws.mergeCells(0,0,9,0); //合併單元格00列到0行第9

     ws.addCell(new jxl.write.Label(0, 0, "查詢結果",format1)); //插入標題

     wwb.write();   //將數據寫入所見的excel

     wwb.close();

     os.close(); // 關閉流

}catch (Exception e) {

     e.printStackTrace();

}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章