java handles exported merged cells through POI

import java.io.IOException;

 

import org.apache.poi.hssf.usermodel.HSSFCell;

import org.apache.poi.hssf.usermodel.HSSFCellStyle;

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.Region;

 

 

public class ExcelTest {    

   

    /**   

     * @param args   

     */   

    public static void main(String[] args) throws IOException {    

   

        try {    

            HSSFWorkbook wb = new HSSFWorkbook();    

            HSSFSheet sheet = wb.createSheet("new   sheet");    

            HSSFCellStyle style = wb.createCellStyle(); // style object    

   

            style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 垂直    

            style.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 水平    

            HSSFRow row = sheet.createRow((short) 0);    

            HSSFRow row2 = sheet.createRow((short) 1);    

   

            sheet.addMergedRegion(new Region(0, (short) 0, 1, (short) 0));    

            HSSFCell ce = row.createCell((short) 0);    

            ce.setEncoding(HSSFCell.ENCODING_UTF_16);// Chinese processing    

            ce.setCellValue("Item\\Date"); // The data displayed in the first row and first column of the table    

            ce.setCellStyle(style); // style, centered    

            int num = 0;    

            for (int i = 0; i < 9; i++) { // loop 9 times, each time showing across cells    

                // Calculate the span from that cell to that cell    

                int celln = 0;    

                int one = 0;    

                if (i == 0) {    

                    celln = 0;    

                    that = 1;    

                } else {    

                    celln = (i * 2);    

                    that = (i * 2 + 1);    

                }    

                // merge cells    

                // The four parameters are: start row, start column, end row, end column    

                sheet.addMergedRegion(new Region(0, (short) (celln + 1), 0,    

                        (short) (the one + 1)));    

                HSSFCell cell = row.createCell((short) (celln + 1));    

                cell.setCellValue("merging" + i); // data displayed across cells    

                cell.setCellStyle(style); // style    

                // Data that does not display across cells, such as: divided into two lines, the previous line is divided into two cells, the next line is two cells, "quantity", "amount"    

                HSSFCell cell1 = row2.createCell((short) celle);    

                HSSFCell cell2 = row2.createCell((short) (celle + 1));    

                cell1.setEncoding(HSSFCell.ENCODING_UTF_16);    

                cell1.setCellValue("数量");    

                cell1.setCellStyle(style);    

                cell2.setEncoding(HSSFCell.ENCODING_UTF_16);    

                cell2.setCellValue("Amount");    

                cell2.setCellStyle(style);    

                num++;    

            }    

   

            // add the total percentage after    

   

            // The total is added at the end, but also across a cell    

            sheet.addMergedRegion(new Region(0, (short) (2 * num + 1), 0,    

                    (short) (2 * num + 2)));    

            HSSFCell cell = row.createCell((short) (2 * num + 1));    

            cell.setEncoding(HSSFCell.ENCODING_UTF_16);    

            cell.setCellValue("Total");    

            cell.setCellStyle(style);    

            HSSFCell cell1 = row2.createCell((short) (2 * num + 1));    

            HSSFCell cell2 = row2.createCell((short) (2 * num + 2));    

            cell1.setEncoding(HSSFCell.ENCODING_UTF_16);    

            cell1.setCellValue("数量");    

            cell1.setCellStyle(style);    

            cell2.setEncoding(HSSFCell.ENCODING_UTF_16);    

            cell2.setCellValue("Amount");    

            cell2.setCellStyle(style);    

   

            // Percentage as above    

            sheet.addMergedRegion(new Region(0, (short) (2 * num + 3), 0,    

                    (short) (2 * num + 4)));    

            HSSFCell cellb = row.createCell((short) (2 * num + 3));    

            cellb.setEncoding(HSSFCell.ENCODING_UTF_16);    

           

            cellb.setCellValue("percentage");    

            cellb.setCellStyle(style);    

            

            HSSFCell cellb1 = row2.createCell((short) (2 * num + 3));    

            HSSFCell cellb2 = row2.createCell((short) (2 * num + 4));    

            cellb1.setEncoding(HSSFCell.ENCODING_UTF_16);    

            cellb1.setCellValue("数量");    

            cellb1.setCellStyle(style);    

            cellb2.setEncoding(HSSFCell.ENCODING_UTF_16);    

            cellb2.setCellValue("Amount");    

            cellb2.setCellStyle(style);    

   

            /***This is the crux of the problem. Writing this workbook to a stream can output the corresponding name. It is ok to write the path here.

            FileOutputStream fileOut = new FileOutputStream("workbook.xls");    

            wb.write(fileOut);    

            fileOut.close();

             **/

            

            

            /**The second is the name of the excel output to the surface

             * pName="Column statistics table";   

    response.reset();   

    response.setContentType("application/x-msdownload");   

    response.setHeader("Content-Disposition","attachment; filename="+new String(pName.getBytes("gb2312"),"ISO-8859-1")+".xls");   

    ServletOutputStream outStream=null;   

  

    try{   

        outStream = response.getOutputStream();   

        wb.write(outStream);   

    }catch(Exception e)   

    {   

     e.printStackTrace ();   

    }finally{   

        outStream.close();   

    }   

             * */

            System.out.print("OK");    

        } catch (Exception ex) {    

            ex.printStackTrace();    

        }    

   

    }  

    

    

    /**  

     * Merge Cells  

     * @param sheet The sheet of excel to merge cells

     * @param cellLine the column to merge  

     * @param startRow the start row of the columns to be merged  

     * @param endRow end row to merge columns  

     */  

    private static void addMergedRegion(HSSFSheet sheet, int cellLine, int startRow, int endRow,HSSFWorkbook workBook){   

           

     HSSFCellStyle style = workBook.createCellStyle(); // style object    

     

        style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 垂直    

        style.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 水平    

        //Get the data of the first row for later comparison    

        String s_will = sheet.getRow(startRow).getCell(cellLine).getStringCellValue();   

     

        int count = 0;

        boolean flag = false;

        for (int i = 1; i <= endRow; i++) {   

         String s_current = sheet.getRow(i).getCell(0).getStringCellValue(); 

         if(s_will.equals(s_current))

         {

          s_will = s_current;

          if(flag)

          {

           sheet.addMergedRegion(new CellRangeAddress(startRow-count,startRow,cellLine,cellLine));

           HSSFRow row = sheet.getRow(startRow-count);

           String cellValueTemp = sheet.getRow(startRow-count).getCell(0).getStringCellValue(); 

           HSSFCell cell = row.createCell(0);

           cell.setCellValue(cellValueTemp); // data displayed across cells    

                  cell.setCellStyle(style); // style    

           count = 0;

           flag = false;

          }

          startRow=i;

          count++;          

         }else{

          flag = true;

          s_will = s_current;

         }

  //Because the merged cells in the above loop are done when there is the same cell next time, if there are the same cells in several rows at the end, the merged cells below will be run.

         if(i==endRow&&count>0)

         {

          sheet.addMergedRegion(new CellRangeAddress(endRow-count,endRow,cellLine,cellLine));   

          String cellValueTemp = sheet.getRow(startRow-count).getCell(0).getStringCellValue(); 

          HSSFRow row = sheet.getRow(startRow-count);

       HSSFCell cell = row.createCell(0);

       cell.setCellValue(cellValueTemp); // data displayed across cells    

                cell.setCellStyle(style); // style    

         }

        }

    }

   

}  

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326485512&siteId=291194637