Location>code7788 >text

springboot-excle file to achieve the export of the same contents of the cell merger

Popularity:158 ℃/2024-10-24 17:29:18

Export excel file in the cell some need to merge how to operate!

For example, the table on the left would like to merge the cells into a table on the right for easier viewing.

         

I. Dependencies

 <!-- exclemanipulate-->
        <dependency>
            <groupId></groupId>
            <artifactId>hutool-all</artifactId>
            <version>5.3.8</version>
        </dependency>

        <dependency>
            <groupId></groupId>
            <artifactId>jxl</artifactId>
            <version>2.6.10</version>
        </dependency>

        <dependency>
            <groupId></groupId>
            <artifactId>poi</artifactId>
            <version>4.1.1</version>
        </dependency>

        <dependency>
            <groupId></groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>4.1.1</version>
        </dependency>

II. Codes

@RequestMapping("/merge")
public class MergeController {

    @RequestMapping(value = "/excleExport" )
    @ResponseBody
    public void csvExport(HttpServletResponse response){
        //Table header
        List<String> headerList = ("No.", "Name");
        // create writer by tool class, default create xls format
        ExcelWriter writer = ();
        // Write to table header
        (headerList);
        //Organizational data
        // Organizational data
        List<List<Object>> data = new ArrayList<>();

        List list1 = ("001","Zhang San");
        List list2 = ("001","Zhang San");
        List list3 = ("001","Zhang San");
        List list4 = ("002","Li Si.");
        List list5 = ("002","Li Si.");
        List list6 = ("003","Wang Wu");
        (list1);
        (list2);
        (list3);
        (list4);
        (list5);
        (list6);

        // Write the content at once, use the default style, force the output of the title
        (data,true);
        //Set the form width automatically
        ();

        // Get the underlying Workbook and Sheet objects.
        Workbook workbook = ();
        Sheet sheet = (0);

        //Merge cells with the same number
        int startRow = 1; // Data start row (skip table header)
        for (int i = 1; i < (); i++) {
            if (!(i).get(0).equals((i - 1).get(0))) {
                ((i).get(0));
                ((i - 1).get(0));
                if (startRow < i) {
                    // Merge numbered columns First two parameters start and end rows Last two parameters start and end columns
                    (new CellRangeAddress(startRow, i , 0, 0));
                    // Merge name columns
                    (new CellRangeAddress(startRow, i , 1, 1));
                }
                startRow = i+1;
            }
        }

        //response is an HttpServeltReponse object
        ("application/-excel;charset=utf-8");

        ("Content-Disposition","attachment;filename=");
        ServletOutputStream out= null;
        try{
            out = ();
            (out,true);

        }catch (IOException e){
            ();
        }finally {
            // Close the writer and free the memory.
            ();
        }
        (out);
    }
    
}

III. postman call

After the download is the beginning of the merged cell effect

Source code access (free):
(1) Login-Register:/
(2) Search: springboot export excel + exported excel cell merge