1.プロジェクトにログ情報をエクスポートします
2。プロジェクトでPOI-*。JARなどのExcelファイルを操作するJARファイルをインポートします
Excelエクスポートは、前景条件に応じてパラメーターをコントローラーに渡し、パラメーターに従ってデータベースを照会し、リストコレクションを照会し、拡張データを生成してリストコレクションをダウンロードします。
コードスニペット:
contorller.java
/***エクスポート情報* @Param Model*/@RequestMapping( "ExportCustomer.Do")@systemControllerLog(description = "Database form exportexcle")public void exportcustomer(modelmap model){// todo //データをエクスポートに取得するリスト<cmcustomer> cuslist = customerservice.exportcustomer(model); //メソッドを使用して、拡張テンプレートスタイルhssfworkbook workbook = customerservice.createexcel(cuslist、request)を生成します。 SimpleDateFormat format = new SimpledateFormat( "yyyymmddhhmmss"); //ファイル名形式を定義するtry {// excle name iso-8859-1を定義して、文字列msg = new String( "customer information_" + format.format(new date()) + ".xls")。getBytes()、 "ISO-8859-1"); //エクスポート時間をファイル名のresponse.setContentType( "Application/vnd.ms-Excel"); Response.Addheader( "Content-Disposition"、 "Attachment; filename =" + msg); workbook.write(Response.getOutputStream()); } catch(ioException e){logger.error(e); }} 2。サービスのcreateexcelメソッド
public hssfworkbook createexcel(list <cmcustomer> cuslist、httpservletrequest request){// excelファイルhssfworkbook workbook = new hssfworkbook(); // excelファイルのシートに対応するWebbookにシートを追加しますhssfsheetシート= workbook.createsheet( "Customer Information Table"); //列幅Sheet.setColumnWidth(0、25 * 100)を設定します。 Sheet.SetColumnWidth(1、35 * 100); Sheet.setColumnWidth(2、35 * 100); Sheet.setColumnWidth(3、40 * 100); Sheet.setColumnWidth(4、45 * 100); Sheet.setColumnWidth(5、45 * 100); Sheet.setColumnWidth(6、50 * 100); Sheet.setColumnWidth(7、80 * 100); Sheet.setColumnWidth(8、35 * 100); Sheet.setColumnWidth(9、40 * 100); //シートのヘッダーの行0を追加しますhssfrow row = sheet.createrow(0); //セルを作成し、テーブルヘッダーを設定し、テーブルヘッダーを中央hssfcellstyle style = workbook.createcellstyle()に設定します。 // Centered Format style.setAlignment(hssfcellstyle.align_center)を作成します。 // border style.setborderbottom(hssfcellstyle.border_thin); //フォントを生成hssffont font = workbook.createfont(); // font肥厚font.setboldweight(hssffont.boldweight_bold); //フォントサイズfont.setfontheightinpoints((short)12); //フォントを現在のスタイルに適用します。SetFont(font); //列全体を中央に設定するか、別々に左に設定しますhssfcellstyle style1 = workbook.createcellstyle(); style1.setalignment(hssfcellstyle.align_center); hssfcellstyle style2 = workbook.createcellstyle(); style2.setalignment(hssfcellstyle.align_left); hssfcellstyle style3 = workbook.createcellstyle(); style3.setalignment(hssfcellstyle.align_left); hssffont hssffont = workbook.createfont(); hssffont.setcolor(hssffont.color_red); hssffont.setboldweight(hssffont.boldweight_bold); style3.setfont(hssffont); hssfcellstyle style4 = workbook.createcellstyle(); style4.setalignment(hssfcellstyle.align_left); hssffont hssffont1 = workbook.createfont(); hssffont1.setcolor(hssffont.color_normal); hssffont1.setboldweight(hssffont.boldweight_bold); style4.setfont(hssffont1); hssfcell cell = row.createcell(0); cell.setCellValue( "シリアル番号"); Cell.SetCellStyle(スタイル); cell = row.createcell(1); cell.setCellValue( "customer name"); Cell.SetCellStyle(スタイル); cell = row.createcell(2); cell.setCellValue( "ジェンダー"); Cell.SetCellStyle(スタイル); cell = row.createcell(3); cell.setCellValue( "ステータス"); Cell.SetCellStyle(スタイル); cell = row.createcell(4); cell.setCellValue( "電話"); Cell.SetCellStyle(スタイル); cell = row.createcell(5); cell.setCellValue( "Mailbox"); Cell.SetCellStyle(スタイル); cell = row.createcell(6); cell.setCellValue( "アドレス"); Cell.SetCellStyle(スタイル); for(int i = 0; i <cuslist.size(); i ++){string logtypedis = ""; row = sheet.createrow(i + 1); cmcustomer cmcustomer = cuslist.get(i); //セルを作成して値を設定します//番号付き列は左にありますhssfcell c1 = row.createcell(0); c1.setcellstyle(style2); c1.setcellvalue(i); hssfcell c2 = row.createcell(1); c2.setcellstyle(style1); c2.setcellvalue(cmcustomer.getCustomername()); // Customer name String sexstr = cmcustomer.getSex(); // gender 0:female、1:male string sex = ""; if( "1" .equals(sexstr)){sex = "MALE"; } if( "0" .equals(sexstr)){sex = "femal"; } hssfcell c3 = row.createcell(2); // gender c3.setcellstyle(style1); c3.setcellvalue(sex); string statusstr = cmcustomer.getStatus(); //顧客ステータス1。On-the-job、2。leave string status = ""; if( "1" .equals(statusstr)){status = "on-the-job"; } if( "2" .equals(statusstr)){status = "stign"; } hssfcell c4 = row.createcell(3); // status c4.setcellstyle(style1); c4.setCellValue(ステータス); string customerId = cmcustomer.getCustomerid(); // customer ID list <cmphone> phoneList = cmphonemapper.selectbycustomerid(customerID);文字列phone = ""; if(phoneList!= null && phoneList.size()> 0){for(int j = 0; j <phoneList.size(); j ++){phone = phoneList.get(j).getPhone(); }} hssfcell c5 = row.createcell(4); // telect c5.setcellstyle(style1); c5.setcellvalue(電話);リスト<Cmemail> emaillist = cmemailmapper.selectall(customerId);文字列email = ""; if(emaillist!= null && emaillist.size()> 0){for(int j = 0; j <emaillist.size(); j ++){email = emaillist.get(j).getemail(); }} hssfcell c6 = row.createcell(5); // emailbox c6.setcellstyle(style1); c6.setCellValue(電子メール); cmaddress cmaddress = new cmaddress(); cmaddress.setcustomerid(customerID);リスト<CMADDRESS> ADDERSSLIST = CMADDRESSMAPPER.SELECTALL(CMADDRESS);文字列adders = ""; if(addersslist!= null && addersslist.size()> 0){for(int j = 0; j <addersslist.size(); j ++){adderss = addersslist.get(j).getaddress(); }} hssfcell c7 = row.createcell(6); //アドレスc7.setcellstyle(style1); c7.setcellvalue(adders); //デフォルトのフォーマットrow.createcell(1).setCellValue(cmcustomer.getCustomername());を使用します。 row.createcell(2).setcellvalue(sex); row.createcell(3).setCellValue(status); row.createcell(4).setCellValue(電話); row.createcell(5).setCellValue(email); row.createcell(6).setCellValue(adders); } return workbook; } 3。ページJSPコール
//情報関数exporbtn(){$ .ajax({type: "post"、url: "<%= path%>/customer/exportcustomer.do"、success:function(data){window.open( '<%>/customer/exportcustomer.do');}}); }上記はこの記事のすべての内容です。みんなの学習に役立つことを願っています。誰もがwulin.comをもっとサポートすることを願っています。