SoFunction
Updated on 2025-03-09

Java method to export Excel using poi

This example shares the specific code of using poi to export Excel for your reference. The specific content is as follows

package ;

import ;
import ;
import ;
import ;
import ;

import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;

/**
  * Excel export tool class
  * @author ts
  */

public class ExportExcelUtil {
 //The title of the export table displayed private String title;
 //Export the column name of the table private String[] rowName ;
 
 private List<Object[]> dataList = new ArrayList<Object[]>();
 
// HttpServletResponse response;
 
 //Construction method, pass in the data to be exported public ExportExcelUtil(String title,String[] rowName,List<Object[]> dataList){
   = dataList;
   = rowName;
   = title;
 }
   
 /*
   * Export data
   * */
 public void export() throws Exception{
  try{
   HSSFWorkbook workbook = new HSSFWorkbook();      // Create a workbook object   HSSFSheet sheet = (title);      // Create worksheets   
   // Generate table title row   HSSFRow rowm = (0);
   HSSFCell cellTiltle = (0);
   (25);           //Set the default line height of the title line   
   //sheet style definition [getColumnTopStyle()/getStyle() are both custom methods - below - Extensible]   HSSFCellStyle columnTitleStyle = (workbook);//Get the title line style   HSSFCellStyle columnTopStyle = (workbook); //Get the column header style object   HSSFCellStyle style = (workbook);     //Cell style object   
   (new CellRangeAddress(0, 1, 0, (-1))); 
   (columnTitleStyle);
   (title);
   
   // Define the number of columns required   int columnNum = ;
   HSSFRow rowRowName = (2);    // Create a row at the location of index 2 (the second row at the beginning of the top row)   (25);       //Set the column header default row height   // Set the column header into the cell of the sheet   for(int n=0;n<columnNum;n++){
    HSSFCell cellRowName = (n);    //Create the number of cells corresponding to the column header    (HSSFCell.CELL_TYPE_STRING);    //Set the data type of the column header cell    HSSFRichTextString text = new HSSFRichTextString(rowName[n]);
    (text);         //Set the value of the column header cell    (columnTopStyle);      //Set the column header cell style   }
   
   //Set the query data into the cell corresponding to the sheet   for(int i=0;i<();i++){
    
    Object[] obj = (i);//Transfer each object    HSSFRow row = (i+3);//Create the required number of rows    (20); // Set the created row to default row height    for(int j=0; j<; j++){
     HSSFCell cell = null; //Set the data type of the cell     if(j == 0){
      cell = (j,HSSFCell.CELL_TYPE_NUMERIC);
      (i+1); 
     }else{
      cell = (j,HSSFCell.CELL_TYPE_STRING);
      if(!"".equals(obj[j]) && obj[j] != null){
       (obj[j].toString());      //Set the value of the cell      }
     }
     (style);         //Set cell style    }
   }
   //Let the column width automatically adapt with the exported column length   for (int colNum = 0; colNum < columnNum; colNum++) {
    int columnWidth = (colNum) / 256;
    for (int rowNum = 0; rowNum < (); rowNum++) {
     HSSFRow currentRow;
     //The current line has not been used     if ((rowNum) == null) {
      currentRow = (rowNum);
     } else {
      currentRow = (rowNum);
     }
     if ((colNum) != null) {
      HSSFCell currentCell = (colNum);
      if (() == HSSFCell.CELL_TYPE_STRING) {
       int length = ().getBytes().length;
       if (columnWidth < length) {
        columnWidth = length;
       }
      }
     }
    }
    if(colNum == 0){
     (colNum, (columnWidth-2) * 256);
     
    }else{
     (colNum, (columnWidth+4) * 256);
    }
   }
   
   if(workbook !=null){
    try
    {
     String fileName = title + ("yyyy-MM-dd") + ".xls";
 // Because the response has been encapsulated into a tool class, the following code is commented out//     response =();
//     ("APPLICATION/OCTET-STREAM");
//     ("Content-Disposition", headStr);
//     OutputStream out = ();
     
     //Solve Chinese garbled code     ().setCharacterEncoding("UTF-8");
     //Solve Chinese garbled code     ().setHeader("Content-Disposition", "attachment;filename*=UTF-8''" + (fileName,"UTF-8"));
     //Download file     ().setContentType("APPLICATION/OCTET-STREAM");
     OutputStream out = ().getOutputStream();
     (out);
    }
    catch (IOException e)
    {
     ();
    }
   }

  }catch(Exception e){
   ();
  }
  
 }
 
 /*
   * Set the title style
   */ 
  public HSSFCellStyle getTitleTopStyle(HSSFWorkbook workbook) {
   
   // Set font   HSSFFont font = ();
   //Set font size   ((short)24);
   //The font is thicker   (HSSFFont.BOLDWEIGHT_BOLD);
   //Set font name   ("Songyi");
   //Set style;   HSSFCellStyle style = ();
   //Set the bottom border;   (HSSFCellStyle.BORDER_THIN);
   //Set the color of the bottom border;   ();
   //Set the left border;   (HSSFCellStyle.BORDER_THIN);
   //Set the color of the left border;   ();
   //Set the right border;   (HSSFCellStyle.BORDER_THIN);
   //Set the color of the right border;   ();
   //Set the top border;   (HSSFCellStyle.BORDER_THIN);
   //Set the top border color;   ();
   //Font set in style with the application;   (font);
   //Set automatic line wrap;   (false);
   //Set the horizontal alignment style to center alignment;   (HSSFCellStyle.ALIGN_CENTER);
   //Set the vertical alignment style to center alignment;   (HSSFCellStyle.VERTICAL_CENTER);
   
   return style;
   
  }
 
 
 /*
   * Column header cell style
   */ 
  public HSSFCellStyle getColumnTopStyle(HSSFWorkbook workbook) {
   
   // Set font   HSSFFont font = ();
   //Set font size   ((short)11);
   //The font is thicker   (HSSFFont.BOLDWEIGHT_BOLD);
   //Set font name   ("Songyi");
   //Set style;   HSSFCellStyle style = ();
   //Set the bottom border;   (HSSFCellStyle.BORDER_THIN);
   //Set the color of the bottom border;   ();
   //Set the left border;   (HSSFCellStyle.BORDER_THIN);
   //Set the color of the left border;   ();
   //Set the right border;   (HSSFCellStyle.BORDER_THIN);
   //Set the color of the right border;   ();
   //Set the top border;   (HSSFCellStyle.BORDER_THIN);
   //Set the top border color;   ();
   //Font set in style with the application;   (font);
   //Set automatic line wrap;   (false);
   //Set the horizontal alignment style to center alignment;   (HSSFCellStyle.ALIGN_CENTER);
   //Set the vertical alignment style to center alignment;   (HSSFCellStyle.VERTICAL_CENTER);
   
   return style;
   
  }
  
  /*
   * Column data information cell style
   */ 
  public HSSFCellStyle getStyle(HSSFWorkbook workbook) {
   // Set font   HSSFFont font = ();
   //Set font size   //((short)10);
   //The font is thicker   //(HSSFFont.BOLDWEIGHT_BOLD);
   //Set font name   ("Songyi");
   //Set style;   HSSFCellStyle style = ();
   //Set the bottom border;   (HSSFCellStyle.BORDER_THIN);
   //Set the color of the bottom border;   ();
   //Set the left border;   (HSSFCellStyle.BORDER_THIN);
   //Set the color of the left border;   ();
   //Set the right border;   (HSSFCellStyle.BORDER_THIN);
   //Set the color of the right border;   ();
   //Set the top border;   (HSSFCellStyle.BORDER_THIN);
   //Set the top border color;   ();
   //Font set in style with the application;   (font);
   //Set automatic line wrap;   (false);
   //Set the horizontal alignment style to center alignment;//   (HSSFCellStyle.ALIGN_CENTER);
   //Set the vertical alignment style to center alignment;//   (HSSFCellStyle.VERTICAL_CENTER);
   
   
   return style;
  
  }
 
}

Implementation method

/*
 * Implementation method
 */
public void exportExcel() throws Exception{
  String title = "test";
  String[] rowsName = new String[]{"Serial Number","Character 1","Character 2","Character 3","Board 4","Character 5"};
  List<Object[]> dataList = new ArrayList<Object[]>();
  Object[] objs = null;
  for (int i = 0; i < 10; i++) {
   objs = new Object[];
   objs[0] = i;
   objs[1] = "Test 1";
   objs[2] = "Test 2";
   objs[3] = "Test 3";
   objs[4] = "Test 4";
   objs[5] = "Test 5";
   (objs);
  }
  ExportExcelUtil ex = new ExportExcelUtil(title, rowsName, dataList);
  ();
  
 }

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.