Original: Java exported Excel code demo instance via Apache POI
Source code: Http://www.zuidaima.com/share/1550463470062592.htm
See download for details, required jar, demo test example, full comment.
Package Com.zuidaima.excel.util;import Java.io.bytearrayinputstream;import Java.io.bytearrayoutputstream;import Java.io.filenotfoundexception;import Java.io.fileoutputstream;import Java.io.ioexception;import Java.io.inputstream;import Java.io.outputstream;import Java.util.arraylist;import Java.util.Calendar;import Java.util.date;import Java.util.map;import Org.apache.poi.hssf.usermodel.hssfcell;import Org.apache.poi.hssf.usermodel.hssfcellstyle;import Org.apache.poi.hssf.usermodel.hssffont;import Org.apache.poi.hssf.usermodel.hssfrow;import Org.apache.poi.hssf.usermodel.hssfsheet;import org.apache.poi.hssf.usermodel.hssfworkbook;/** * Description is responsible for exporting the dataset (form data) to an Excel file * @author www.zuidaima.com */public class Excelutil {/** * class instance */private static excelutil export; /** * Excel Document */private Hssfworkbook workbook; /** * Excel sheet */private hssfsheet sheet; /** * Byte stream */private outputstream fileOutput; /** * Declaring private constructor method */PRIVate Excelutil () {}/** * generates an Excel Export tool class instance (singleton mode) * * @return Excel Export Tool Class object */public static EXCE Lutil newinstance () {if (export = = null) export = new Excelutil (); return export; }/** * @ Feature description set Excel document (Single form) * @ Creator www.zuidaima.com * @ creation time 2011-5-27 pm 02:25:58 * @param tname * Excel table Name set * @param Theader * Excel Header DataSet * @param tValue * Excel form data set (except header) * @param Theaderstyle * Excel header cell style * @param tvaluestyle * Excel form data cell style (except header) * @param FilePath * Excel file Address * @throws Exception * Exception thrown up */public void Export Excel (String tname, arraylist<string> theader, arraylist<arraylist<object>> tValue, Map<St Ring, short> Theaderstyle, map<string, short> Tvaluestyle, String filePath) throws Exception {try { Created when an Excel document does not existworkbook = new Hssfworkbook (); Single-form assignment and style this.setsheet (Tname, Theader, TValue, Theaderstyle, Tvaluestyle); Export Excel file This.export (workbook, FilePath); } catch (Exception e) {e.printstacktrace (); }}/** * @ Feature description set Excel document (Single form) * @ Creator www.zuidaima.com * @ creation time 2011-5-27 pm 02:25:58 * @param tname * Excel table Name set * @param theader * Excel header Data set * @param tValue * Excel form DataSet ( Except header) * @param theaderstyle * Excel header cell style * @param tvaluestyle * Excel form data cell style (except header) * @throws Exception * Exception thrown up */public InputStream Exportexceltostream (String tname, ArrayList <String> Theader, arraylist<arraylist<object>> tValue, map<string, short> Theaderstyle, Map<string, short> Tvaluestyle) throws Exception {//Create workbook when Excel document does not exist = new HSSFWorkbook (); Single-form assignment and style this.setsheet (Tname, Theader, TValue, Theaderstyle, Tvaluestyle); Export Excel file return export (workbook); }/** * @ Feature description set Excel document (multiple form) * @ Creator www.zuidaima.com * @ creation time 2011-5-27 pm 02:25:58 * @param tname * Excel table Name set * @param Theader * Excel Header DataSet * @param tValue * Excel form data set (except header) * @param Theaderstyle * Excel header cell style * @param tvaluestyle * Excel form data cell style (except header) * @param FilePath * Excel file Address * @throws Exception * Exception thrown up */public void Export Excel (arraylist<string> tname, arraylist<arraylist<string>> Theader, ARRAYLIST<ARRAYLIST&L T Arraylist<object>>> TValue, arraylist<map<string, short>> Theaderstyle, ArrayList<Ma P<string, short>> Tvaluestyle, String filePath) throws Exception {try {//when EXThe cel document does not exist when you create workbook = new Hssfworkbook (); The For loop completes the assignment and style for each form of the document for (int i = 0; i < tname.size (); i++) {This.setsheet (Tname.get (i), t Header.get (i), tvalue.get (i), theaderstyle.get (i), tvaluestyle.get (i)); Single-form assignment and style}//export Excel file This.export (workbook, FilePath); } catch (Exception e) {}}/** * @ Feature description set Excel document (multiple form) * @ Creator www.zuidaima.com * @ creation time 2011-5-27 Afternoon 02:25:58 * @param tname * Excel table Name set * @param Theader * Excel Header DataSet * @param tval UE * Excel form data set (except header) * @param theaderstyle * Excel header cell style * @param tvaluestyle * Excel form data cell style (except header) * @throws Exception * Exception thrown up */public InputStream Exportexcelt OStream (arraylist<string> tname, arraylist<arraylist<string>> Theader, ArrayList<ArrayList <arraylist<object>>> TValue, arraylist<map<string, short>> Theaderstyle, arraylist<map<string, Short>> Tvaluestyle) throws Exception {//When an Excel document does not exist, create workbook = new Hssfworkbook (); The For loop completes the assignment and style for each form of the document for (int i = 0; i < tname.size (); i++) {This.setsheet (Tname.get (i), THEADER.G ET (i), tvalue.get (i), theaderstyle.get (i), tvaluestyle.get (i)); Single-form assignment and style} return export (workbook); }/** * @ Feature description set Excel form * @ Creator www.zuidaima.com * @ creation time 2011-5-27 pm 02:23:02 * @param tname * Excel table name * @param Theader * Excel Header DataSet * @param tValue * Excel form data set (except header) * @param Theaderstyle * Excel header cell style * @param tvaluestyle * Excel form data cell style (except for header) * @thr OWS Exception * Exception thrown up */private void Setsheet (String tname, arraylist<string> Theader, Arra Ylist<arraylist<object>> TValue, Map<string, short> Theaderstyle, map<string, short> Tvaluestyle) throws Exception {try { Create a form and set its table name sheet = workbook.createsheet (tname); Create a form line hssfrow Trow = sheet.createrow (0); Assignment and style (at this point, the header row) Trow = This.settrow (Trow, Theader, Theaderstyle); For loop complete assignment and style of form data (except header) for (int i = 0; i < tvalue.size (); i++) {Trow = Sheet.createrow (i + 1); Get form line Trow = This.settrow (Trow, Tvalue.get (i), tvaluestyle); Sets the data and style of the current row}} catch (Exception e) {e.printstacktrace (); }}/** * @ Feature description set Excel table row data * @ Creator www.zuidaima.com * @ creation time 2011-5-27 pm 02:22:30 * @param row * Excel form line * @param trow * Excel table single row data * @param Theaderstyle * Excel header Style * @return the set of form lines * @throws Exception * Exception thrown out */@SuppressWArnings ("unchecked") Private Hssfrow Settrow (hssfrow row, ArrayList Trow, map<string, short> Theaderstyle) throws Exception {try {//Get cell style Hssfcellstyle CellStyle = This.setcellstyle (Theaderstyle); Declare cell Hssfcell cell = null; The For loop completes the form of a row for each column assignment and style for (int i = 0; i < trow.size (); i++) {cell = Row.createcell (i); Gets the cell Cell.setcellstyle (CellStyle) for each column; Set Style Sheet.autosizecolumn ((short) i); Set cell adaptive Object obj = Trow.get (i); Gets the value of the column//determines what type the object belongs to, and strongly turns if (obj instanceof Integer)//when the number cell.setc Ellvalue ((Integer) obj); if (obj instanceof string)//When the string is Cell.setcellvalue ((string) obj); if (obj instanceof Boolean)//When Boolean Cell.setcellvalue ((Boolean) obj); if (obj instanceof date)//When Time is Cell.setcellvalue ((Date) obj); if (obj instanceof Calendar)//When Time is Cell.setcellvalue ((Calendar) obj); if (obj instanceof Double)//When the decimal is Cell.setcellvalue ((Double) obj); }} catch (Exception e) {e.printstacktrace (); } return row; return}/** * @ Function Description Set cell style * @ Creator www.zuidaima.com * @ creation time 2011-5-27 pm 02:21:40 * @param fontstyle * Style Map Collection * @return Set cell style * @throws Exception * Exception thrown out */private Hssfcel Lstyle Setcellstyle (map<string, short> fontstyle) throws Exception {//Declaration cell style Hssfcellstyle Cellstyl e = null; try {//Create a font Hssffont font = Workbook.createfont (); Set the font style//Set font color (red: hssffont.color_red here means short type) Font.setcolor (fontstyle.get ("color")); Set the font shape (wide body: hssffont.boldweight_bold 700) --Bold Font.setboldweight (Fontstyle.get ("weight")); Create cell style CellStyle = Workbook.createcellstyle (); Add a font style cellstyle.setfont (font); } catch (Exception e) {e.printstacktrace (); } return CellStyle; return}/** * @ Feature description export Excel * @ Creator www.zuidaima.com * @ creation time 2011-5-27 pm 02:57:37 * @param workbook * Excel Document * @param filePath * xls file address * @throws Exception * Exception thrown out */private void Export (Hssfworkbook workbook, String FilePath) throws Exception {try {//based on specified XLS Create a file character stream fileOutput = new FileOutputStream (FilePath); Writes a document to the specified file Workbook.write (fileOutput); } catch (FileNotFoundException e) {e.printstacktrace (); } catch (Exception e) {e.printstacktrace (); } finally {try {//close stream, free resources Fileoutput.close (); } catch (IOException e) {e.printstacktrace (); ; }}}/** * @ function Description Get stream * @ Creator www.zuidaima.com * @ creation time 2011-5-27 pm 02:57:37 * @param workbook * Excel Document * @throws Exception * Exception thrown out */private InputStream export (Hssfworkboo K Workbook) throws IOException {Bytearrayoutputstream BAOs = new Bytearrayoutputstream (); try {try {workbook.write (BAOs); } catch (IOException e) {e.printstacktrace (); } byte[] ba = Baos.tobytearray (); Bytearrayinputstream Bais = new Bytearrayinputstream (BA); return Bais; } finally {//closes the stream, releasing the resource Baos.close (); } }}
Java Export Excel code demo instance via Apache POI