Export excel spreadsheet poi

 

Front section of code

//导出按钮
<a class="btn btn-app" onclick="exportExcel(${applyParticipants.get(0).cid})"><i class="fa fa-edit"></i>导出Excel</a>

// js code portions (only to do a jump)
<Script type = "text / JavaScript">
// Export Excel
function exportExcel (CID) {
    // Go to open tab
window.location.href = "toSignUp ? / Export CID = "CID +;
}
</ Script>

Background code section

// Controller layer
/ ** 
* @Description
* export function
* Create Excel
* @author luying
*
* /

@ RequestMapping (value = "/ Export")
@ResponseBody
public void Export (Request the HttpServletRequest, HttpServletResponse the Response, Integer cid) throws Exception {
the try
{
ExcelUtil = new new ExcelUtil excelUtil ();
      // column names, excel table first row of data
String [] rowsName = { "ID", "name", "whether the team contact", "registration type", "ID number" "telephone", "location", "work unit" and "taxpayer number", "work unit", "e-mail"};
      // create a collection, the collection of data stored to be displayed in excel
List < Object []> dataCode0 = new ArrayList <Object []> ();
= New new ApplyParticipants zjgl ApplyParticipants ();
      // get the sql statement required by data
List <ApplyParticipants> applyList = toSignUpService.selectPersonMessageByPrimaryKey (CID);
IF (!! = Null && dbDatas dbDatas.isEmpty ()) {
            // Initialization Object [] array
Object [] = ab & null;
            // for loop, in the applyList data into Object [] array
for (applyParticipants applyParticipants: applyList) {
ab & = new new Object [. 11];
ab & [0] = applyParticipants.getPid ();
ab & [. 1] = applyParticipants.getPname ();
ab & [2] applyParticipants.getTeamcontact = ();
ab & [. 3] = applyParticipants.getRegistrationtypes ();
ab & [. 4] = applyParticipants.getIdnum ();
ab & [. 5] = applyParticipants.getPhone ();
ab & [. 6] = applyParticipants.getArea ();
              // not empty is determined (isEmpty companies themselves encapsulation method), if no data is displayed No
if (applyParticipants.getWorkunits ( ) == null || applyParticipants.getWorkunits () isEmpty ()) {.
ab & [. 7] = "no";
} the else {
ab & [. 7] = applyParticipants.getWorkunits ();
}

IF (applyParticipants.getRatepayer () = null =) {
ab & [. 8] = "no";
} the else {
ab & [. 8] = applyParticipants.getRatepayer ();
}
(. applyParticipants.getJob () == null || applyParticipants.getJob () isEmpty ()) {IF
ab & [. 9] = "No";
} the else {
ab & [. 9] = applyParticipants.getJob ();
}
IF ( . applyParticipants.getEmail () == null || applyParticipants.getEmail () isEmpty ()) {
ab & [10] = "no";
} the else {
ab & [10] = applyParticipants.getEmail ();
}
              // data Object [] ab placed in List <Object []> dataCode0 in
dataCode0.add (ab &);
}
}
      // definition table name
String title = "Registration details of personal information table";
      // definition file name
String fileName = new String ( "personal information registration table details .xls" .getBytes ( "UTF-8"), "UTF-8");
       // entering excelUtil in exportExcel () method
excelUtil.exportExcel (title, rowsName, dataCode0, fileName, Response);
}
the catch (UnsupportedEncodingException E)
{
e.printStackTrace ();
}
}

// used tools excelUtil.class (which information seems to be no need to change, when the concrete used in the judgment)

com.jusfoun.uums.util Package;

Import the org.apache.poi.hssf.usermodel *;.

Import javax.servlet.http.HttpServletResponse;
Import a java.io.OutputStream;
Import java.io.UnsupportedEncodingException;
Import Classes in java.util. List;

// Import java.io.OutputStream;
// Import java.io.UnsupportedEncodingException;
// Import javax.servlet.http.HttpServletResponse;

/ **
*
*
*
* @author wxy
* @version [version number, 2019 February 1]
* /
public class ExcelUtil
{
/ **
* export Excel
* @param title title export table
* @param column names rowsName export table
* @param dataList need to export data
* @param fileName 生成excel文件的文件名
* @param response
*/
public void exportExcel(String title, String[] rowsName, List<Object[]> dataList, String fileName, HttpServletResponse response)
{
//创建HSSFWorkbook
//响应到客户端
try {
HSSFWorkbook wb = this.export(title, rowsName, dataList, fileName);
this.setResponseHeader(response, fileName);
OutputStream os = response.getOutputStream();
wb.write(os);
os.flush();
os.close();
} catch (Exception e) {
e.printStackTrace();
}
}

public void setResponseHeader(HttpServletResponse response, String fileName)
{
try
{
try
{
fileName = new String(fileName.getBytes(), "ISO8859-1");
}
catch (UnsupportedEncodingException e)
{
e.printStackTrace();
}
response.setContentType("application/octet-stream;charset=UTF-8");
response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
response.addHeader("Pargam", "no-cache");
response.addHeader("Cache-Control", "no-cache");
}
the catch (Exception EX)
{
ex.printStackTrace ();
}
}

/ *
* export data
* /
Private HSSFWorkbook Export (title String, String [] rowName, List <Object []> dataList, String fileName)
throws Exception
{

HSSFWorkbook Workbook = new HSSFWorkbook (); // create a workbook objects
HSSFSheet sheet = workbook.createSheet (title); // Create worksheet
int columnNum = rowName.length; // define the required number of columns
HSSFRow rowRowName = sheet.createRow (0); // Create a row (top row) at the index position 0
// column head is provided to the cell 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
HSSFRichTextString text = new HSSFRichTextString (rowName [ n] );
cellRowName.setCellValue (text); // set the value of the column header cells
}

// query the data set 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
for (int j = 0; j <obj .length; J ++)
{
HSSFCell cell = null; // set the cell data type
IF (J == 0)
{
= row.createCell Cell (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
}
}
}
}

// long column so that the column width with the derived automatically adapted
for (colNum int = 0; colNum <columnNum; colNum ++)
{
int = sheet.getColumnWidth the columnWidth (colNum) / 256;
for (int rowNum = 0; rowNum < sheet.getLastRowNum(); rowNum++)
{
HSSFRow currentRow;
// 当前行未被使用过
if (sheet.getRow(rowNum) == null)
{
currentRow = sheet.createRow(rowNum);
}
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 (colNum == 0)
{
sheet.setColumnWidth(colNum, (columnWidth - 2) * 256);
}
else
{
sheet.setColumnWidth(colNum, (columnWidth + 4) * 256);
}
}
return workbook;
}
}
 

Guess you like

Origin www.cnblogs.com/luYing666/p/11590556.html