プロジェクトの要件により、データはExcelテーブルにエクスポートする必要があり、エクスポートアイテムを選択してダウンロードできます。プロジェクトで使用されるSpring+MyBatis+SpringMVCフレームワークは、Apache POIを使用してExcelをエクスポートするために使用されます。 BaiduでPOIを使用してください。これ以上苦労せずに、コードを追加してください。
Exportexcelutilコード
パッケージcom.rixin.common.util;インポートjava.io.outputStream; Import java.lang.reflect.method; import java.net.urlencoder; Import java.util.collection; Import java.util.iterator; import javax.servlet.http.httptservletresponse; Import; org.apache.poi.hssf.usermodel.HSSFCell;import org.apache.poi.hssf.usermodel.HSSFCellStyle;import org.apache.poi.hssf.usermodel.HSSFRow;import org.apache.poi.hssf.usermodel.HSSFSheet;import org.apache.poi.hssf.usermodel.hssfworkbook; import org.apache.poi.ss.usermodel.font;/** * JavaeeエクスポートExcelツールクラス * * @author [email protected] * @see poi */publicクラスエクスポーチルexprestil * * * * * * * paperutill FILENAME *「学生テーブル」 * @Param Excelheader * Excel Table Header配列、「名前#name」フォーマット文字列を保存し、「名前」はExcelヘッダーの行、「名前」はオブジェクトフィールド名 * @param Datalist *データ収集です。 */public static <t> hssfworkbook export(httpservletresponse応答、string filename、string [] excelheader、collection <t> datalist)スロー例外{// request Response.setContentType( "Application/Application/vnd.ms-Excel"); Response.setheader( "content-disposition"、 "attachment; filename =" + urlencoder.encode(filename + ".xls"、 "utf-8")); // Excelファイルに対応するワークブックを作成しますhssfworkbook wb = new hssfworkbook(); //タイトルスタイルを設定hssfcellstyle titlestele = wb.createcellstyle(); //セルボーダースタイルのtitleStyle.setbordertop(hssfcellstyle.border_thin); //上部ボーダー薄いエッジラインTitlestele.setborderbottom(hssfcellstyle.border_thin); titlestele.setborderright(hssfcellstyle.border_thin); //右の境界薄いエッジライン//セットセルアライメントtitlestele.setalignment(hssfcellstyle.align_center); // Horizontal Center TitleStyle.setverticalAlignment(hssfcellstyle.vertical_center); // Vertical Center // Font Style Font TitleFont = wb.createfont(); titlefont.setfontheightinpoints((short)15); // font height titlefont.setFontName( "bold"); // font style titlestele.setfont(titlefont); // Excelファイルのシートに対応するワークブックにシートを追加しますhssfsheetシート= wb.createsheet(filename); //タイトル配列文字列[] titlearray = new String [ExcelHeader.Length]; //フィールド名配列文字列[] fieldArray = new String [ExcelHeader.Length]; for(int i = 0; i <excelheader.length; i ++){string [] temparray = excelheader [i] .split( "#"); //一時的な配列split#titlearray [i] = temparray [0]; fieldArray [i] = temparray [1]; } //シートにタイトル行を追加するhssfrow row = sheet.createrow((int)0); //行数は0から始まりますhssfcell sequencecell = row.createcell(0); SequenceCell.setCellStyle(TitleStyle); sheet.autosizecolumn(0); // widthを自動的に設定しますtitlecell.setCellStyle(TitleStyle); sheet.autosizecolumn(i + 1); // 0ビットはシリアル番号によって占有されているため、+1} //データスタイルは異なるタイトルとデータスタイルのために個別に設定する必要があります。 //データボーダーDataStyle.setborderBottom(hssfcellstyle.border_thin); dataStyle.setbordertop(hssfcellstyle.border_thin); dataStyle.setborderLeft(hssfcellstyle.border_thin); dataStyle.setborderright(hssfcellstyle.border_thin); //センタースタイルのdataStyle.setAlignment(hssfcellstyle.align_center)を設定します。 // Horizontal Center DataStyle.setverticalAlignment(hssfcellstyle.vertical_center); // Vertical Center //データフォントフォントdatafont = wb.createfont(); datafont.setfontheightinpoints((short)12); // font height datafont.setFontName( "安"); // font dataStyle.setFont(dataFont); //コレクションデータをトラバースしてデータを生成するrow iterator <t> it = datalist.iterator(); int index = 0; while(it.hasnext()){index ++; // 0ビットが占有されているため、+1 row = sheet.createrow(index); //シーケンス番号に値を割り当てますhssfcell sequencecellvalue = row.createcell(0); //シーケンス番号値は常に0番目の列SequenceCellValue.setCellValue(index); SequenceCellValue.setCellStyle(DataStyle); sheet.autosizecolumn(0); t t =(t)it.next(); //反射を使用して、渡されたフィールド名配列に基づいて対応するgetxxx()メソッドを動的に呼び出し、(int i = 0; i <fieldArray.length; i ++){hssfcell datacell = row.createcell(i+1)の属性値を取得します。 datacell.setCellStyle(DataStyle); sheet.autosizecolumn(i + 1); string fieldname = fieldArray [i]; string getMethodName = "get" + fieldName.substring(0、1).touppercase() + fieldname.substring(1); //対応するgetxxx()メソッドクラスを取得<?オブジェクト> tcls = t.getClass(); // genericsはすべてのオブジェクトのオブジェクトとサブクラスです。 datacell.setCellValue(value.toString()); //現在の列に値を割り当てますoutputStream.close(); // close stream Return wb; } // XSSFCELLSTYLE.ALIGN_CENTER CENTER ALIGNMENT // XSSFCELLSTYLE.ALIGN_LEFT LEFT ALIGNMENT // XSSFCELLSTYLE.ALIGN_RIGHT RIGHT ALIGNMENT // XSSFCELLSTYLE.VETICAL_TOP ALIGNMENT // XSSFCELLSTYLE.VERTICAL_BOTTOM IN THE RIGHT // CELLSTYLE.BORDER_DOUBLE両面線// CellStyle.Border_Thin Thin Edge // CellStyle.border_Medium Medium Edge // CellStyle.border_Dashed Dotted Edge
コントローラーコード
@RequestMapping( "/ExportProject.Do")public void exportProject(httpservletResponse応答、string export){string [] excelheader = export.split( "、");リスト<Project> ProjectList = ProjectService.getProjects(null); try {exportexcelutil.export(response、 "project table"、excelheader、projectlist); } catch(Exception e){e.printstacktrace(); }} JSPコードの一部
... <div style = "マージントップ:5px;"> <span> <入力タイプ= "チェックボックス"値 "値="プロジェクト名#name name = "export" checked "checked"> </span <input type = "text" value = "project name" disabled "> </div> ...
下の写真
上記はこの記事に関するものです。すべての人の学習に役立つことを願っています。