Home > Back-end >  Do it himself a export into excel file tool guide
Do it himself a export into excel file tool guide

Time:04-17

-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- XSSFWorkbook tool, the method can be inherited and then rewrite into account for the public is -- -- -- -- -- -- -- -- -- -- -- --
The import com. Alibaba. Fastjson. JSONArray;

The import org, apache poi. Ss. Usermodel. BorderStyle;
The import org, apache poi. Ss. Usermodel. CellStyle;
The import org, apache poi. Ss. Usermodel. HorizontalAlignment;
The import org, apache poi. Ss. Usermodel. VerticalAlignment;
The import org, apache poi. Ss. Util. CellRangeAddress;
The import org, apache poi. XSSF. Usermodel. XSSFCell;
The import org, apache poi. XSSF. Usermodel. XSSFCellStyle;
The import org, apache poi. XSSF. Usermodel. XSSFFont;
The import org, apache poi. XSSF. Usermodel. XSSFRow;
The import org, apache poi. XSSF. Usermodel. XSSFSheet;
The import org, apache poi. XSSF. Usermodel. XSSFWorkbook;

import java.io.IOException;
The import java.net.URLEncoder;
Import the Java. Util. Base64;

The import javax.mail. Servlet. ServletOutputStream;
The import javax.mail. Servlet. HTTP. It;
import javax.servlet.http.HttpServletResponse;

The import com. Alibaba. Fastjson. JSONObject;

The import org. Springframework. Web. Context. Request. RequestContextHolder;
The import org. Springframework. Web. Context. Request. ServletRequestAttributes;

Public class XSSWorkbookUtil {

/* *
* the next line number, the current line number=row - 1
*/
Int row=0;

/* *
* file name
*/
String fileName="export data. XLSX";

XSSFWorkbook workbook=null;

Public XSSWorkbookUtil (String fileName) {
This. The fileName=fileName;
Workbook=new XSSFWorkbook ();
}

Public XSSWorkbookUtil () {
Workbook=new XSSFWorkbook ();
}

/* *
* general style
*
*/
Public XSSFCellStyle createBorderStyle () {
XSSFCellStyle cellStyle=workbook. CreateCellStyle ();
CellStyle. SetBorderBottom (BorderStyle. THIN);//bottom border
CellStyle. SetBorderLeft (BorderStyle. THIN);//left margin
CellStyle. SetBorderTop (BorderStyle. THIN);//border
CellStyle. SetBorderRight (BorderStyle. THIN);//right border
CellStyle. SetAlignment (HorizontalAlignment. CENTER);//horizontal center
CellStyle. SetVerticalAlignment (VerticalAlignment. CENTER);//vertical center
Return the cellStyle;
}

/* *
* heading styles
*
*/
Public XSSFCellStyle createTitleStyle () {
XSSFCellStyle cellStyle=createBorderStyle ();
XSSFFont font=workbook. CreateFont ();
font.setBold(true);
The font. SetFontName (" tahoma ");
The font. SetFontHeight ((short) 300);
CellStyle. SetFont (font);
Return the cellStyle;
}

/* *
* create since the growth lines
*
* @ param sheet - page
*/
Public XSSFRow createrow (XSSFSheet sheet) {
XSSFRow res=sheet. CreateRow (row);
Row++;
return res;
}

/* *
* create a named style column
*
* @ param res - which lines
* @ param cellStyle - style
* @ param columnIndex - which column
*/
Public XSSFCell createCel (XSSFRow res, CellStyle CellStyle, int columnIndex) {
XSSFCell cel=res. CreateCell (columnIndex);
Cel. SetCellStyle (cellStyle);
Return cel.
}

/* *
* create merging style title
*
* @ param sheet - page
* @ param cellStyle - heading styles
* @ param columnNum - merge rows
*/
Public XSSFCell createTitleRow (XSSFSheet sheet, CellStyle CellStyle, int columnNum) {
1://merge cell parameters starting line parameters 2: terminate the line parameter 3:4: start column parameters end columns
XSSFRow res=createrow (sheet);
Sheet. AddMergedRegion (new CellRangeAddress (row row - 1, 1, 0, columnNum));
XSSFCell cel=null;
for (int i=0; I & lt; ColumnNum + 1; I++) {
XSSFCell tempcel=res. CreateCell (I);
Tempcel. SetCellStyle (cellStyle);
If (I==0)
Cel=tempcel;
}
Return cel.
}

/* *
* create a working table
*
* @ param list worksheet data
* @ param sheetName work table name
* @ return
*/
Public Boolean setSheetData (JSONArray list, String sheetName) {
If (null==list | | list. The size ()==0)
return false;
The row=0;
//style
XSSFCellStyle cellBorderStyle=createBorderStyle ();
XSSFSheet sheet=null;
If (sheetName==null | | sheetName. IsEmpty ()) {
Sheet=workbook. CreateSheet ();
} else {
Sheet=workbook. CreateSheet (sheetName);
}
XSSFRow temprow=createrow (sheet);
Object [] cols=null;
for (int i=0; I & lt; List. The size (); I++) {
JSONObject checkSchool=(JSONObject) list. Get (I);
If (I==0) {
Cols=checkSchool. KeySet (.) toArray ();
CreateCel (temprow cellBorderStyle, 0). SetCellValue (" serial number ");
for (int j=0; J & lt; Cols. Length; J++) {
CreateCel (temprow cellBorderStyle, j + 1) setCellValue (cols [j]. The toString ());
}
}
Temprow=createrow (sheet);
CreateCel (temprow cellBorderStyle, 0). SetCellValue (I + 1);
for (int j=0; J & lt; Cols. Length; J++) {
Object colData=https://bbs.csdn.net/topics/checkSchool.get (cols [j]. The toString ());
CreateCel (temprow cellBorderStyle, j + 1) setCellValue (colData=https://bbs.csdn.net/topics/=null?" ": colData. ToString ());
}
}
return true;
}

nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related