最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

jxl 導(dǎo)出數(shù)據(jù)到excel的實例講解

 更新時間:2017年12月12日 15:28:45   作者:侯爵吖  
下面小編就為大家分享一篇jxl 導(dǎo)出數(shù)據(jù)到excel的實例講解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧

優(yōu)點:

Jxl對中文支持非常好,操作簡單,方法看名知意。

Jxl是純javaAPI,在跨平臺上表現(xiàn)的非常完美,代碼可以再windows或者Linux上運行而無需重新編寫

支持Excel 95-2000的所有版本(網(wǎng)上說目前可以支持Excel2007了,還沒有嘗試過)

生成Excel 2000標(biāo)準(zhǔn)格式

支持字體、數(shù)字、日期操作

能夠修飾單元格屬性

支持圖像和圖表,但是這套API對圖形和圖表的支持很有限,而且僅僅識別PNG格式。

缺點:

效率低,圖片支持不完善,對格式的支持不如POI強大

案例:

String times = (new SimpleDateFormat("yyyyMMddHHmmss")).format(new Date());
String fname = "系統(tǒng)日志" + times; // 文件名
List<Logs> list=logsService.selectForList(hql.toString());
  String path = request.getSession().getServletContext().getRealPath("/")
    + "xls/"
    + (new SimpleDateFormat("yyyyMMdd")).format(new Date());
  File file = new File(path);
  // 如果文件夾不存在則創(chuàng)建
  if (!file.exists() && !file.isDirectory()) {
   file.mkdir();
  }
  response.setContentType("application/vnd.ms-excel;charset=utf-8");// // 指定文件的保存類型。
  response.setCharacterEncoding("utf-8");
  ExportUtil.writer_log(request,fname, list, response);//下載到本地

writer_log導(dǎo)出方法如下

/**
  * 生成 excel 文件,導(dǎo)出到本地電腦
  * @param fname 文件名
  * @param list 需要打印的數(shù)據(jù),即數(shù)據(jù)庫查詢的數(shù)據(jù)列表
  */
 public static void writer_log(HttpServletRequest request,String fname, List list, HttpServletResponse response) {
  try {
   OutputStream os = response.getOutputStream();//取得輸出流
   response.reset();//清空輸出流
   // 下面是對中文文件名的處理 開始
   response.setCharacterEncoding("UTF-8");//設(shè)置相應(yīng)內(nèi)容的編碼格式
   if(isMsBrowser(request))
    fname= java.net.URLEncoder.encode(fname ,"UTF-8");
    else fname = new String(fname.getBytes("UTF-8"),"ISO-8859-1");
   response.setHeader("Content-Disposition","attachment;filename="+fname+".xls");
   response.setContentType("application/msexcel;charset=utf-8");//定義輸出類型
   // 對中文文件名的處理 結(jié)束
   
   // 此處的 Workbook 導(dǎo)入的是 import jxl.Workbook;
   WritableWorkbook wbook = Workbook.createWorkbook(os); // 建立excel文件
   WritableSheet sheet = wbook.createSheet("系統(tǒng)日志", 0); // 工作表名稱
   
   CellView cellView = new CellView(); 
   cellView.setAutosize(true); //設(shè)置自動大小
   sheet.setColumnView(0, 8); //設(shè)置單元格寬度,0是列號,8是寬度
   sheet.setColumnView(1, 20); //設(shè)置單元格寬度,1是列號,20是寬度
   sheet.setColumnView(2, 24);
   sheet.setColumnView(3, 20);
   sheet.setColumnView(4, 30);
   sheet.setColumnView(5, 13);
   sheet.setColumnView(6, 15);
   sheet.setColumnView(7, 32);
   sheet.setColumnView(8, 15);
   
   
   // 設(shè)置Excel字體
   WritableFont wfont = new WritableFont(WritableFont.createFont("宋體"), 22,
     WritableFont.BOLD, false,
     jxl.format.UnderlineStyle.NO_UNDERLINE,
     jxl.format.Colour.BLACK);        //設(shè)置單元格字體樣式
   WritableCellFormat titleFormat = new WritableCellFormat(wfont); //添加單元格字體
   titleFormat.setAlignment(Alignment.CENTRE);      //設(shè)置文字居中對齊方式; 
   String[] title = { "系統(tǒng)日志" };
   // 設(shè)置Excel表頭 開始
   for (int i = 0; i < title.length; i++) {
    // 此處導(dǎo)入的是 import jxl.write.Label;
    Label excelTitle = new Label(i, 0, title[i], titleFormat); //單元格內(nèi)容
    // 參數(shù)順序:開始列,開始行,結(jié)束列,結(jié)束行
    sheet.mergeCells(0, 0, 8, 0);        //所在位置,第幾行第幾列
    sheet.addCell(excelTitle);         //添加單元格信息
   }
   // 設(shè)置Excel表頭 結(jié)束
   // 第一行,即顯示時間,參數(shù):(所在列,所在行,內(nèi)容)
   WritableFont wfonttime = new WritableFont(WritableFont.createFont("宋體"), 11,
     WritableFont.NO_BOLD, false,
     jxl.format.UnderlineStyle.NO_UNDERLINE,
     jxl.format.Colour.BLACK);        //設(shè)置單元格字體樣式
   WritableCellFormat titletime = new WritableCellFormat(wfonttime);//添加單元格字體
   titletime.setAlignment(Alignment.RIGHT);      //設(shè)置文字居中對齊方式; 
   DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
//   sheet.setColumnView(1, cellView);        //根據(jù)內(nèi)容自動設(shè)置列寬 
   Label contentDate = new Label(0, 1, df.format(new Date()), titletime); //單元格內(nèi)容
//   sheet.mergeCells(16, 1, 18, 1);     //所在位置,第幾行第幾列,即合并的位置,如沒合并,可不寫
   sheet.addCell(contentDate);      //添加單元格信息
   // 第一行 結(jié)束
   // 第二行,顯示條件標(biāo)題欄
   WritableFont wfont2 = new WritableFont(WritableFont.createFont("宋體"), 11,
     WritableFont.BOLD, false,
     jxl.format.UnderlineStyle.NO_UNDERLINE,
     jxl.format.Colour.BLACK);        //設(shè)置單元格字體樣式
   WritableCellFormat titleFormat2 = new WritableCellFormat(wfont2);//添加單元格字體
   titleFormat2.setBorder(Border.ALL, BorderLineStyle.THIN);   //設(shè)置邊框--實線; 
   titleFormat2.setAlignment(Alignment.CENTRE);      //設(shè)置文字居中對齊方式; 
   titleFormat2.setVerticalAlignment(VerticalAlignment.CENTRE);  //設(shè)置垂直居中; 
   Label content2a = new Label(0, 2, "序號", titleFormat2);   //單元格內(nèi)容--第1格
   Label content2b = new Label(1, 2, "用戶名", titleFormat2);  //單元格內(nèi)容--第2格
   Label content2c = new Label(2, 2, "記錄時間", titleFormat2);  //單元格內(nèi)容--第3格
   Label content2d = new Label(3, 2, "操作模塊", titleFormat2);   //單元格內(nèi)容--第4格
   Label content2e = new Label(4, 2, "操作內(nèi)容", titleFormat2);  //單元格內(nèi)容--第5格
   Label content2f = new Label(5, 2, "操作動作", titleFormat2);   //單元格內(nèi)容--第6格
   Label content2g = new Label(6, 2, "操作人IP", titleFormat2);   //單元格內(nèi)容--第7格
   Label content2h = new Label(7, 2, "所屬組織", titleFormat2);   //單元格內(nèi)容--第8格
   Label content2i = new Label(8, 2, "備注", titleFormat2);  //單元格內(nèi)容--第9格
   sheet.mergeCells(0, 1, 8, 1);
   /*sheet.mergeCells(0, 2, 0, 4);  //所在位置,第幾行第幾列,即合并的位置,如沒合并,可不寫;(此處為第3行開始到5行,第1列)
   sheet.mergeCells(1, 2, 1, 4);  //所在位置,第幾行第幾列,即合并的位置,如沒合并,可不寫;(此處為第3行開始到5行,第2列)
   sheet.mergeCells(2, 2, 2, 4);  //所在位置,第幾行第幾列,即合并的位置,如沒合并,可不寫;(此處為第3行開始到5行,第3列)
   sheet.mergeCells(3, 2, 3, 4);  //所在位置,第幾行第幾列,即合并的位置,如沒合并,可不寫;(此處為第3行開始到5行,第4列)
   sheet.mergeCells(4, 2, 4, 4);  //所在位置,第幾行第幾列,即合并的位置,如沒合并,可不寫;(此處為第3行開始到5行,第5列)
   sheet.mergeCells(5, 2, 5, 4);  //所在位置,第幾行第幾列,即合并的位置,如沒合并,可不寫;(此處為第3行開始到5行,第6列)
   sheet.mergeCells(6, 2, 6, 4);  //所在位置,第幾行第幾列,即合并的位置,如沒合并,可不寫;(此處為第3行開始到5行,第7列)
   sheet.mergeCells(7, 2, 7, 4);  //所在位置,第幾行第幾列,即合并的位置,如沒合并,可不寫;(此處為第3行開始到5行,第8列)
   sheet.mergeCells(8, 2, 8, 4);  //所在位置,第幾行第幾列,即合并的位置,如沒合并,可不寫;(此處為第3行開始到5行,第9列)
   sheet.mergeCells(9, 2, 9, 4);  //所在位置,第幾行第幾列,即合并的位置,如沒合并,可不寫;(此處為第3行開始到5行,第10列)
   sheet.mergeCells(10, 2, 12, 3);  //所在位置,第幾行第幾列,即合并的位置,如沒合并,可不寫;(此處為第3行開始到4行,第11-13列)
   sheet.mergeCells(13, 2, 15, 3);  //所在位置,第幾行第幾列,即合并的位置,如沒合并,可不寫;(此處為第3行開始到4行,第14-16列)
   sheet.mergeCells(16, 2, 16, 4);  //所在位置,第幾行第幾列,即合并的位置,如沒合并,可不寫;(此處為第3行開始到5行,第17列)
   sheet.mergeCells(17, 2, 17, 4);  //所在位置,第幾行第幾列,即合并的位置,如沒合并,可不寫;(此處為第3行開始到5行,第18列)
   sheet.mergeCells(18, 2, 18, 4);  //所在位置,第幾行第幾列,即合并的位置,如沒合并,可不寫;(此處為第3行開始到5行,第19列)
*/   sheet.addCell(content2a);   //添加第1格單元格信息
   sheet.addCell(content2b);   //添加第2格單元格信息
   sheet.addCell(content2c);   //添加第3格單元格信息
   sheet.addCell(content2d);   //添加第4格單元格信息
   sheet.addCell(content2e);   //添加第5格單元格信息
   sheet.addCell(content2f);   //添加第6格單元格信息
   sheet.addCell(content2g);   //添加第7格單元格信息
   sheet.addCell(content2h);   //添加第8格單元格信息
   sheet.addCell(content2i);   //添加第9格單元格信息
   WritableFont wf = new WritableFont(WritableFont.createFont("宋體"), 11,
     WritableFont.NO_BOLD, false,
     jxl.format.UnderlineStyle.NO_UNDERLINE,
     jxl.format.Colour.BLACK);      //設(shè)置單元格字體樣式
   WritableCellFormat wcf = new WritableCellFormat(wf); //添加單元格字體
   wcf.setBorder(Border.ALL, BorderLineStyle.THIN);   //設(shè)置邊框--實線; 
   wcf.setVerticalAlignment(VerticalAlignment.CENTRE);  //設(shè)置垂直對齊
   wcf.setAlignment(Alignment.CENTRE);      //設(shè)置文字水平居中對齊方式;
   wcf.setWrap(true);          //自動換行
   
   WritableFont wf1 = new WritableFont(WritableFont.createFont("宋體"), 11,
     WritableFont.NO_BOLD, false,
     jxl.format.UnderlineStyle.NO_UNDERLINE,
     jxl.format.Colour.BLACK);      //設(shè)置單元格字體樣式
   WritableCellFormat wcf1 = new WritableCellFormat(wf1); //添加單元格字體
   wcf1.setBorder(Border.LEFT, BorderLineStyle.THIN);   //設(shè)置邊框--實線; 
   wcf1.setVerticalAlignment(VerticalAlignment.CENTRE);  //設(shè)置垂直對齊
   wcf1.setAlignment(Alignment.CENTRE);      //設(shè)置文字水平居中對齊方式;
   
   // 以下循環(huán)數(shù)據(jù)庫獲取的信息
   int c = 1; // 用于循環(huán)時Excel的行號
   Iterator it = list.iterator();
   while (it.hasNext()) {
    Logs tc = (Logs) it.next();
    DateFormat dfmt = new SimpleDateFormat("yyyy.MM");
    
    String xh = String.valueOf(c);
    if(xh== null){
     xh = "";
    }
    Label content0 = new Label(0, c+2, xh, wcf);       //序號
    
    String name = tc.getUserName();
    if(name == null){
     name = "";
    }
    Label content1 = new Label(1, c+2, name, wcf);      //用戶名
    
    String xrPresent = tc.getLogTime();
    if(xrPresent == null){
     xrPresent = "";
    }
    Label content2 = new Label(2, c+2, xrPresent, wcf);     //記錄時間
    
    String czModel = tc.getModel();
    if(czModel == null){
     czModel = "";
    }
    Label content3 = new Label(3, c+2, czModel, wcf);     //操作模塊
    
    String sex = tc.getContent();
    if(sex == null){
     sex = "";
    }
    Label content4 = new Label(4, c+2, sex, wcf);       //操作內(nèi)容
    
    String birthday = tc.getOperate();
    if(birthday == null){
     birthday = "";
    }
    Label content5 = new Label(5, c+2, birthday, wcf);     //操作動作
    
    String nation = tc.getIp();
    if(nation == null){
     nation = "";
    }
    Label content6 = new Label(6, c+2, nation, wcf);      //操作人IP
    
    String origin = tc.getOrgName();
    if(origin == null){
     origin = "";
    }
    Label content7 = new Label(7, c+2, origin, wcf);      //所屬組織
    
    String bPlace = tc.getRemark();
    if(bPlace == null){
     bPlace = "";
    }
    Label content8 = new Label(8, c+2, bPlace, wcf);      //備注
    
    String abc="";
    Label content9 = new Label(9, c+2, abc, wcf1);      //備注
    
    
    sheet.setRowView(c+2, 600); // 設(shè)置行高
    sheet.setRowView(c+2, 600);
    sheet.setRowView(c+2, 600);
    sheet.setRowView(c+2, 600);
    sheet.setRowView(c+2, 600);
    sheet.setRowView(c+2, 600);
    sheet.setRowView(c+2, 600);
    sheet.setRowView(c+2, 600);
    sheet.setRowView(c+2, 600);
    sheet.setRowView(c+2, 600);
    sheet.mergeCells(0, c+2, 0, c+2); // 合并第一列第c+2行到第一列第c+2行的所有單元格
    sheet.mergeCells(1, c+2, 0, c+2); //mergeCells(a,b,c,d) 單元格合并函數(shù)
    sheet.mergeCells(2, c+2, 0, c+2); //a 單元格的列號
    sheet.mergeCells(3, c+2, 0, c+2); //b 單元格的行號
    sheet.mergeCells(4, c+2, 0, c+2); //c 從單元格[a,b]起,向下合并到c列
    sheet.mergeCells(5, c+2, 0, c+2); //d 從單元格[a,b]起,向下合并到d行
    sheet.mergeCells(6, c+2, 0, c+2);
    sheet.mergeCells(7, c+2, 0, c+2);
    sheet.mergeCells(8, c+2, 0, c+2);
    sheet.mergeCells(9, c+2, 0, c+2);
    sheet.addCell(content0);
    sheet.addCell(content1);
    sheet.addCell(content2);
    sheet.addCell(content3);
    sheet.addCell(content4);
    sheet.addCell(content5);
    sheet.addCell(content6);
    sheet.addCell(content7);
    sheet.addCell(content8);
    sheet.addCell(content9);
    c++;
   }
   wbook.write(); // 寫入文件
   wbook.close();
   os.close();
  } catch (Exception e) {
   throw new PaikeException("導(dǎo)出文件出錯");
  }
 }

以上這篇jxl 導(dǎo)出數(shù)據(jù)到excel的實例講解就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • SpringBoot項目啟動過程動態(tài)修改接口請求路徑的解決方案

    SpringBoot項目啟動過程動態(tài)修改接口請求路徑的解決方案

    在SpringBoot服務(wù)整合過程中,遇到了多個服務(wù)中相同RequestMapping路徑導(dǎo)致的啟動問題,解決方案是通過修改RequestMappingHandlerMapping類的getMappingForMethod方法,本文給大家介紹SpringBoot修改接口請求路徑的解決方案,感興趣的朋友一起看看吧
    2024-09-09
  • 解決Java & Idea啟動tomcat的中文亂碼問題

    解決Java & Idea啟動tomcat的中文亂碼問題

    這篇文章主要介紹了Java & Idea啟動tomcat的中文亂碼問題,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-07-07
  • Springboot添加支付接口

    Springboot添加支付接口

    這篇文章主要介紹了springboot如何添加支付接口,幫助大家更好得理解和學(xué)習(xí)使用springboot框架,感興趣的朋友可以了解下
    2021-04-04
  • Spring?Boot統(tǒng)一處理全局異常的實戰(zhàn)教程

    Spring?Boot統(tǒng)一處理全局異常的實戰(zhàn)教程

    最近在做項目時需要對異常進行全局統(tǒng)一處理,所以下面這篇文章主要給大家介紹了關(guān)于Spring?Boot統(tǒng)一處理全局異常的相關(guān)資料,文中通過實例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2021-12-12
  • java飛行棋實現(xiàn)思路

    java飛行棋實現(xiàn)思路

    這篇文章主要為大家詳細(xì)介紹了java飛行棋的實現(xiàn)思路,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-09-09
  • 詳解SpringBoot使用RedisTemplate操作Redis的5種數(shù)據(jù)類型

    詳解SpringBoot使用RedisTemplate操作Redis的5種數(shù)據(jù)類型

    本文主要介紹了SpringBoot使用RedisTemplate操作Redis的5種數(shù)據(jù)類型,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-03-03
  • java實現(xiàn)響應(yīng)重定向發(fā)送post請求操作示例

    java實現(xiàn)響應(yīng)重定向發(fā)送post請求操作示例

    這篇文章主要介紹了java實現(xiàn)響應(yīng)重定向發(fā)送post請求操作,結(jié)合實例形式分析了java請求響應(yīng)、重定向及數(shù)據(jù)處理相關(guān)操作技巧,需要的朋友可以參考下
    2020-04-04
  • Java全局異常處理器實現(xiàn)過程解析

    Java全局異常處理器實現(xiàn)過程解析

    這篇文章主要介紹了Java全局異常處理器實現(xiàn)過程解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-02-02
  • Java Swing中的JButton、JComboBox、JList和JColorChooser組件使用案例

    Java Swing中的JButton、JComboBox、JList和JColorChooser組件使用案例

    這篇文章主要介紹了Java Swing中的按鈕(JButton)、組合框(JComboBox)、下拉列表(JList)和顏色選擇器(JColorChooser)組件使用案例,需要的朋友可以參考下
    2014-10-10
  • Java ShutdownHook原理詳解

    Java ShutdownHook原理詳解

    這篇文章主要介紹了Java ShutdownHook原理的相關(guān)資料,幫助大家更好的理解和學(xué)習(xí)使用Java,感興趣的朋友可以了解下
    2021-04-04

最新評論

芜湖市| 景谷| 来宾市| 海门市| 水富县| 隆昌县| 格尔木市| 晋宁县| 罗定市| 定州市| 鄂伦春自治旗| 孟村| 二连浩特市| 永丰县| 抚松县| 泗洪县| 达孜县| 高青县| 兴宁市| 灵丘县| 环江| 长乐市| 永福县| 唐河县| 林甸县| 临漳县| 雷州市| 广元市| 盘锦市| 崇阳县| 永平县| 炎陵县| 襄垣县| 江门市| 渝中区| 砚山县| 鹤山市| 溧阳市| 石林| 龙陵县| 阳新县|