Excel export, directly out of the file selection box, select the files

1. First prepare the jar package

<dependency>
    <groupId> org.apache.poi </ groupId>
    <artifactId> POI </ artifactId>
    <Version> 3.8 </ Version>
</ dependency>
2. front page interface to access export excel

To access the interface via window.location.href, direct download pop-up box

// export data
$ ( '# outExcel') the Click (function () {.
    Var Status = $ ( '# Status') Val ();.
    Window.location.href the baseUrl + =' / Order / Excel / orderrdExportExcel Status? = "+ Status;
});
3. background OutStream stream by obtaining the response, which would excel file to the stream, as follows:

@GetMapping ( "/ orderrdExportExcel")
public void grouppExportExcel (the HttpServletRequest Request, Orderrd orderrd, HttpServletResponse the Response) throws Exception {
    List <Orderrd> grouppList = orderrdService.selectAllForLayUI (orderrd);
    // export the file title
String title = "Refund Order "+ DateUtil.getSimpleDate01 (new new a Date ()) +" XLSX. ";
// set table header rows
String [] headers = new String [ ] {" number "," serial numbers "," user ID "," user name "," accounts receivable "," account balance "," refund amount "," fee "," application time "};
List <Object []> = new new dataList the ArrayList <Object []> ();
Object [ ] OBJS = null;
for (int I = 0; I <grouppList.size (); I ++) {
OBJS = new new Object [headers.length];
OBJS [0] = 0; // set number,Will appear in the class tool
objs [1] = grouppList.get (i ) .getOrderrdCode ();
OBJS [2] = grouppList.get (I) .getUsername ();
OBJS [. 3] = grouppList.get (I) .getUserName ();
OBJS [. 4] = grouppList.get (I) .getAccount ();
OBJS [ . 5] = grouppList.get (I) .getPremain ();
OBJS [. 6] = grouppList.get (I) .getPricePre ();
OBJS [. 7] = grouppList.get (I) .getPricePay ();
OBJS [. 8] grouppList.get = (I) .getCreateTime ();
dataList.add (OBJS); // add to excel spreadsheet data
}
// used to export data stream
the OutputStream OUT = null;
the try {
    // prevent Chinese distortion
String headStr = " Attachment; filename = \ "" + new new String (title.getBytes ( "GB2312"), "ISO8859-1") + "\" ";
the response.setContentType (" octets / Stream ");
the response.setContentType (" the APPLICATION / OCTET-STREAM ");
response.setHeader("Content-Disposition", headStr);
out = response.getOutputStream();
ExportExcelSeedBack ex = new ExportExcelSeedBack(title, headers, dataList);//没有标题
ex.export(out);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}

}
4.工具类如下:


import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

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.HSSFRichTextString;
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.hssf.util.HSSFColor;

public class ExportExcelSeedBack {

//显示的导出表的标题
private String title;
// export table column names
Private String [] rowName;

Private List <Object []> = new new dataList the ArrayList <Object []> ();

the HttpServletResponse Response;


// constructor, the incoming data to be exported
public ExportExcelSeedBack (String title, String [] rowName, List <Object []> dataList) {
this.dataList = dataList;
this.rowName = rowName;
this.title = title;
}

/ *
* export data
* * /
public void export (the OutputStream OUT) Exception {throws
the try {
HSSFWorkbook workbook new new HSSFWorkbook = (); // create a workbook objects
HSSFSheet sheet = workbook.createSheet (title); // Create worksheet

// generated table heading row
// HSSFRow rowm = sheet.createRow (0 );
// HSSFCell cellTiltle = rowm.createCell (0);

{// sheet style definition getColumnTopStyle () / getStyle () method are custom - below - Scalable]
HSSFCellStyle columnTopStyle = this.getColumnTopStyle (workbook); // Get the column head style object
HSSFCellStyle style = this.getStyle (workbook ); // cell style objects

// sheet.addMergedRegion (new CellRangeAddress (0, 1, 0, (rowName.length-1))); // the merged cell
// cellTiltle.setCellStyle (columnTopStyle);
// cellTiltle .setCellValue (title);

// defines the number of columns needed
int = columnNum rowName.length;
HSSFRow rowRowName sheet.createRow = (0); // create a row for the index of 2 (top row of the second line)

// set the column header cells to the sheet
for (n-int = 0; n-<columnNum; n-++) {
HSSFCell cellRowName rowRowName.createCell = (n-); // creates a corresponding number of column header cells
cellRowName.setCellType (HSSFCell.CELL_TYPE_STRING); // set the column header cells datatype
Text = new new HSSFRichTextString HSSFRichTextString (rowName [n-]);
cellRowName.setCellValue (text); // set the value of the column header cells
cellRowName.setCellStyle (columnTopStyle); // Set the column head cell styles
}

// will inquire data provided to the corresponding cell sheet
for (int I = 0; I <dataList.size (); I ++) {
Object [] obj = dataList.get (I); // iterate each object
HSSFRow row = sheet. createRow (i + 1); // Create the required number of rows (second row from the start writing data)

for (int J = 0; J <obj.length; J ++) {
HSSFCell cell = null; // set cell data type
IF (J == 0) {
Cell row.createCell = (J, HSSFCell.CELL_TYPE_NUMERIC);
cell.setCellValue (I +. 1);
} the else {
Cell row.createCell = (J, HSSFCell.CELL_TYPE_STRING);
IF (!. "" the equals (obj [J]) && obj [J] = null!) {
cell.setCellValue (obj [J] .toString ()); // set the cell value
}
}
Cell.setCellStyle (style); // set the cell style
}
}
// length of the column so that the column width with the derived automatically adapt
for (int colNum = 0; colNum <columnNum; colNum ++) {
int = sheet.getColumnWidth the columnWidth (colNum) / 256;
for (int rowNum = 0; rowNum <sheet.getLastRowNum (); rowNum ++) {
HSSFRow currentRow;
// current row has not been used
IF (sheet.getRow (rowNum) == null) {
currentRow = sheet.createRow (rowNum);
} {the else
currentRow = sheet.getRow (rowNum);
}
// IF (! currentRow.getCell (colNum) = null) {
// HSSFCell CurrentCell = currentRow.getCell (colNum);
// IF (currentCell.getCellType () == HSSFCell.CELL_TYPE_STRING) {
// int length = currentCell.getStringCellValue().getBytes().length;
// if (columnWidth < length) {
// columnWidth = length;
// }
// }
// }
if (currentRow.getCell(colNum) != null) {
HSSFCell currentCell = currentRow.getCell(colNum);
if (currentCell.getCellType() == HSSFCell.CELL_TYPE_STRING) {
int length = 0;
try {
length = currentCell.getStringCellValue().getBytes().length;
} catch (Exception e) {
//e.printStackTrace();
}
if (columnWidth < length) {
columnWidth = length;
}
}
}

}
if(colNum == 0){
sheet.setColumnWidth(colNum, (columnWidth-2) * 256);
the else {}
sheet.setColumnWidth (colNum, (the columnWidth +. 4) * 256);
}
}
IF (Workbook = null!) {
the try {
workbook.write (OUT);
} the catch (IOException E) {
e.printStackTrace ();
}
}

} the catch (Exception E) {
e.printStackTrace ();
}
the finally {
the out.close ();
}
}

/ *
* column header cell style
* /
public HSSFCellStyle getColumnTopStyle (HSSFWorkbook Workbook) {

// set the font
HSSFFont font workbook.createFont = ();
// set the font size
font.setFontHeightInPoints ((Short) 11);
// font bold
font.setBoldweight (HSSFFont.BOLDWEIGHT_BOLD);
// set the font name
font.setFontName ( "Courier New");
// set the style;
HSSFCellStyle style = workbook.createCellStyle ();
// set the base frame;
style.setBorderBottom (HSSFCellStyle.BORDER_THIN);
// set the color of the base frame;
style.setBottomBorderColor ( HSSFColor.BLACK.index);
// set the left border;
style.setBorderLeft (HSSFCellStyle.BORDER_THIN);
// set left border color;
style.setLeftBorderColor (HSSFColor.BLACK.index);
// set the right border;
style.setBorderRight ( HSSFCellStyle.BORDER_THIN);
// set the right border color;
style.setRightBorderColor (HSSFColor.BLACK.index);
// set the top border;
style.setBorderTop (HSSFCellStyle.BORDER_THIN);
// set the top border color;
style.setTopBorderColor (HSSFColor .BLACK.index);
// set the font style used in the application;
style.setFont (font);
// Set wrap;
style.setWrapText (to false);
// Sets the horizontal alignment of the pattern is centered;
style.setAlignment (HSSFCellStyle.ALIGN_CENTER);
// Sets the vertical alignment pattern is centered ;
style.setVerticalAlignment (HSSFCellStyle.VERTICAL_CENTER);

return style;

}

/ *
* column data cell style information
* /
public HSSFCellStyle the getStyle (HSSFWorkbook Workbook) {
// set Font
HSSFFont font workbook.createFont = ();
// set the font size
//font.setFontHeightInPoints((short)10);
// font bold
//font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
// set the font name
font.setFontName ( "Courier New");
// set styles;
HSSFCellStyle style = workbook.createCellStyle ();
// Set the bottom border;
style.setBorderBottom (HSSFCellStyle.BORDER_THIN);
// set the color of the bottom border;
style.setBottomBorderColor (HSSFColor.BLACK.index);
// Set the left border;
style.setBorderLeft (HSSFCellStyle.BORDER_THIN);
// set left border color;
style.setLeftBorderColor (HSSFColor.BLACK.index);
// set the right border;
style.setBorderRight (HSSFCellStyle.BORDER_THIN);
// set the right frame color;
style.setRightBorderColor (HSSFColor.BLACK.index);
/ / set the top border;
style.setBorderTop (HSSFCellStyle.BORDER_THIN);
// set the top border color;
style.setTopBorderColor (HSSFColor.BLACK.index);
// set the style font with the application;
style.setFont (font);
/ / set wrap;
style.setWrapText (to false);
// Sets the horizontal alignment of the pattern is centered;
style.setAlignment (HSSFCellStyle.ALIGN_CENTER);
// Sets the vertical alignment pattern is centered;
style.setVerticalAlignment (HSSFCellStyle.VERTICAL_CENTER);

return style;

}



}

 

Guess you like

Origin www.cnblogs.com/Koaler/p/12047349.html