java poi 一个单元格写如多行数据

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java poi 一个单元格写如多行数据相关的知识,希望对你有一定的参考价值。

希望各位大侠帮忙,小弟遇到个问题。希望不吝赐教,急啊!!!

在用java poi 往excel中写数据的时候,怎么能往一个单元格内插入多行数据啊?
就比如 在一个单元格内 出现这样的情况 nihao
china
hello java

期待高手的出现!希望大家帮忙! 谢谢啊!!!
呵呵 是用java 往excel里面写数据 用到了一个apache poi东西
呵呵 谢谢大家不过以上的方法都不行啊
格式就是 这样的
nihao
china
hello java
呵呵 继续期待高手的出现啊

谢谢 宝宝和妖怪 不过客户要求是用poi做的
你说的那个我也看过 JXL对中文的支持比较好

我在其他的地方找到了答案而且试了也好用,把方法分享给大家吧!希望对以后遇到相同问题的其他的人能有帮助
在poi中set cell的时候 用这样的方法就行了
HSSFCell cell;
cell.setcellValue("nihao\n\rchina\n\rhello java");
只要在字符串中加入\n\r就可以了。呵呵 挺简单的吧
鉴于宝宝和妖怪 给的这么多的提示
谢谢你啊 把分都送给你了!

建议你用JXL
当初我做EXCEL的问题的时候
在网上查就发现这两种组件都能解决
不过我选了JXL 方法确实简单
我有例子。。

补充下 客户这东西你可以和他们商量
毕竟很少一部分人才懂技术的 不管哪种技术
思路都是一样的 只是提供的组件不同而已
你那个想插入的数据 只定是查询出来的吧
必然会用到for循环
POI 提供你怎么插入单元格的 你就把你查询出来的
东西放里面就行了 你想多插点 没问题啊
拼嘛 case 0 : ws.addCell(new Label(x, z+1,Sensing.getSampleNameForId(checkData.getSampleId())));
这是JXL 处理单元格时候用到的
看不懂没关系 意思懂就好 。
参考技术A 楼主,请你在左面上新建一个Excel,之后用手向单元格中输入你想要的,看能否实现,如果可以,再想办法解决你的问题。 参考技术B 没使过。
Excel是微软的东西。
应该不会给Java留接口吧。
如果处处留接口,那Office卖谁去。
参考技术C 换行用\n
如果不行就\r\n
如果还不行就<br>

如果还不行,就别把分给我了

Java基于POI动态合并单元格

基于poi动态合并表格

-首先看下效果图

左边为主表数据,右边为子表数据,可以根据自己的需求进行修改,下面来看下代码。

  • 引入依赖
		<!--poi-->
		<dependency>
			<groupId>org.apache.poi</groupId>
			<artifactId>poi-ooxml</artifactId>
			<version>4.1.0</version>
		</dependency>
  • 创建一个PoiExcel类

先构造数据,根据表格,主子表是有相关联的,所以将主子表数据存入一个map集合中,主表的值作为map的key,value对应子表的list集合,可参考下面代码:

        String[] masterHead = "学号","姓名","专业";
        String[] childHead = "课程名称","上课地点","任课教师","上课时间";
        List<String[]> childList = new ArrayList<>();
        childList.add(new String[]"Java程序设计","1号楼302","雷老师","2022/8/30 15:53:49");
        childList.add(new String[]"数据结构","1号楼305","雷老师","2022/8/30 9:18:28");
        List<String[]> childList1 = new ArrayList<>();
        childList1.add(new String[]"计算机网络","2号楼301","方老师","2022/8/30 15:53:49");
        List<Map<String,List<String[]>>> masterList = new ArrayList<>();
        Map<String,List<String[]>> map = new HashMap();
        map.put("20210211-张晓-计算机与科学",childList);
        map.put("20210212-于丽-电子信息工程",childList1);
        masterList.add(map);

然后创建一个Excel工作薄对象

 		//创建Excel工作薄对象
        HSSFWorkbook workbook=new HSSFWorkbook();
        //创建Excel工作表对象
        HSSFSheet sheet = workbook.createSheet("wj");
        //设置单元格居中
        HSSFCellStyle cellStyle = workbook.createCellStyle();
        cellStyle.setAlignment(HorizontalAlignment.CENTER);

然后根据需求一行行来给工作表格填充数据,首先是复杂表头,第一行是主表和子表,主表和子表的是合并列,根据主子表头的长度来确定,合并的列数;第二行是表头,根据主子表头的数组来填充。

		//创建行的单元格,从0开始
        HSSFRow row = sheet.createRow(0);

        //创建统计单元格
        HSSFCell masterCell=row.createCell(0);
        //赋值
        masterCell.setCellValue("主表");
        masterCell.setCellStyle(cellStyle);
        //合并列
        CellRangeAddress region=new CellRangeAddress(0, 0, 0, masterHead.length-1);
        sheet.addMergedRegion(region);

        //创建详情单元格  从统计单元格的后一格开始创建
        HSSFCell childCell = row.createCell(masterHead.length);
        //赋值
        childCell.setCellValue("子表");
        childCell.setCellStyle(cellStyle);
        //合并列
        region=new CellRangeAddress(0, 0, masterHead.length, masterHead.length+childHead.length-1);
        sheet.addMergedRegion(region);

        //表头 从1开始
        HSSFRow titleRow = sheet.createRow(1);
        //主表头
        for (int i = 0; i < masterHead.length ; i++) 
            HSSFCell msCell = titleRow.createCell(i);
            msCell.setCellStyle(cellStyle);
            msCell.setCellValue(masterHead[i]);
        
        //子表头
        for (int i = 0; i < childHead.length; i++) 
            HSSFCell chcell = titleRow.createCell(masterHead.length+i);
            chcell.setCellStyle(cellStyle);
            chcell.setCellValue(childHead[i]);
        

这样就将第一行和第二行的表头填充上了,然后再填充对应的数据,先填充的是主表,主表的值我是用“-”来进行分隔的,所以用字符串进行切割成数组,然后拿到主表数据和对应的子表数据,工作簿的前2行都是表头,所以填充数据是从第三行开始,所以行下标为2,然后从第三行开始创建行,填充主表的数据,填充时要判断下子表的list的大小,大于一才需要进行合并,填充完主表后再填充子表,子表不需要合并,就一行行填充,代码如下:

        //填充数据
        int lastRowIndex = 2; //记录最后行位置
        for (Map<String,List<String[]>> m : masterList)
            for (String key : m.keySet())
                String[] ms = key.split("-");
                List<String[]> chlist = m.get(key);
                HSSFRow valueRow = sheet.createRow(lastRowIndex);
                for (int i = 0; i < ms.length ; i++) 
                    HSSFCell mscell = valueRow.createCell(i);
                    mscell.setCellStyle(cellStyle);
                    mscell.setCellValue(ms[i]);
                    if (chlist.size()>1) //子表数量大于1才进行 行合并
                        region=new CellRangeAddress(lastRowIndex, lastRowIndex+chlist.size()-1, i, i);
                        sheet.addMergedRegion(region);
                    
                
                for (int i = 0; i < chlist.size(); i++) 
                    String[] chstrs = chlist.get(i);
                    HSSFRow chRow;
                    if (i == 0) //避免重复创建 覆盖主表数据
                        chRow = valueRow;
                    else 
                        chRow  = sheet.createRow(lastRowIndex);
                    
                    lastRowIndex++;
                    for (int j = 0; j < chstrs.length; j++) 
                        HSSFCell chcell = chRow.createCell(ms.length+j);
                        chcell.setCellStyle(cellStyle);
                        chcell.setCellValue(chstrs[j]);
                    
                
            
        

其中最重要的一句代码就是:

new CellRangeAddress(int firstRow, int lastRow, int firstCol, int lastCol)

这句代码就是合并单元格,参数1:起始行 参数2:终止行 参数3:起始列 参数4:终止列

  • 最后看下PoiExcel类的完整代码

import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.util.CellRangeAddress;

import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.*;

/**
 * @author huao
 * @Date 2022/8/31 13:50
 * @description:
 */
public class PoiExcel 

    public static void excelport(HttpServletResponse response) throws Exception 

        //数据来源 通过参数传入
        String[] masterHead = "学号","姓名","专业";
        String[] childHead = "课程名称","上课地点","任课教师","上课时间";
        List<String[]> childList = new ArrayList<>();
        childList.add(new String[]"Java程序设计","1号楼302","雷老师","2022/8/30 15:53:49");
        childList.add(new String[]"数据结构","1号楼305","雷老师","2022/8/30 9:18:28");
        List<String[]> childList1 = new ArrayList<>();
        childList1.add(new String[]"计算机网络","2号楼301","方老师","2022/8/30 15:53:49");
        List<Map<String,List<String[]>>> masterList = new ArrayList<>();
        Map<String,List<String[]>> map = new HashMap();
        map.put("20210211-张晓-计算机与科学",childList);
        map.put("20210212-于丽-电子信息工程",childList1);
        masterList.add(map);

        //创建Excel工作薄对象
        HSSFWorkbook workbook=new HSSFWorkbook();
        //创建Excel工作表对象
        HSSFSheet sheet = workbook.createSheet("wj");
        //设置单元格居中
        HSSFCellStyle cellStyle = workbook.createCellStyle();
        cellStyle.setAlignment(HorizontalAlignment.CENTER);

        //创建行的单元格,从0开始
        HSSFRow row = sheet.createRow(0);

        //创建统计单元格
        HSSFCell masterCell=row.createCell(0);
        //赋值
        masterCell.setCellValue("主表");
        masterCell.setCellStyle(cellStyle);
        //合并列
        CellRangeAddress region=new CellRangeAddress(0, 0, 0, masterHead.length-1);
        sheet.addMergedRegion(region);

        //创建详情单元格  从统计单元格的后一格开始创建
        HSSFCell childCell = row.createCell(masterHead.length);
        //赋值
        childCell.setCellValue("子表");
        childCell.setCellStyle(cellStyle);
        //合并列
        region=new CellRangeAddress(0, 0, masterHead.length, masterHead.length+childHead.length-1);
        sheet.addMergedRegion(region);

        //表头 从1开始
        HSSFRow titleRow = sheet.createRow(1);
        //主表头
        for (int i = 0; i < masterHead.length ; i++) 
            HSSFCell msCell = titleRow.createCell(i);
            msCell.setCellStyle(cellStyle);
            msCell.setCellValue(masterHead[i]);
        
        //子表头
        for (int i = 0; i < childHead.length; i++) 
            HSSFCell chcell = titleRow.createCell(masterHead.length+i);
            chcell.setCellStyle(cellStyle);
            chcell.setCellValue(childHead[i]);
        

        //填充数据
        int lastRowIndex = 2; //记录最后行位置
        for (Map<String,List<String[]>> m : masterList)
            for (String key : m.keySet())
                String[] ms = key.split("-");
                List<String[]> chlist = m.get(key);
                HSSFRow valueRow = sheet.createRow(lastRowIndex);
                for (int i = 0; i < ms.length ; i++) 
                    HSSFCell mscell = valueRow.createCell(i);
                    mscell.setCellStyle(cellStyle);
                    mscell.setCellValue(ms[i]);
                    if (chlist.size()>1) //子表数量大于1才进行 行合并
                        region=new CellRangeAddress(lastRowIndex, lastRowIndex+chlist.size()-1, i, i);
                        sheet.addMergedRegion(region);
                    
                
                for (int i = 0; i < chlist.size(); i++) 
                    String[] chstrs = chlist.get(i);
                    HSSFRow chRow;
                    if (i == 0) //避免重复创建 覆盖主表数据
                        chRow = valueRow;
                    else 
                        chRow  = sheet.createRow(lastRowIndex);
                    
                    lastRowIndex++;
                    for (int j = 0; j < chstrs.length; j++) 
                        HSSFCell chcell = chRow.createCell(ms.length+j);
                        chcell.setCellStyle(cellStyle);
                        chcell.setCellValue(chstrs[j]);
                    
                
            
        

        String fileName = URLEncoder.encode("POIExcel下载测试","UTF-8");
        response.setContentType("application/octet-stream;charset=UTF-8");
        response.setHeader("Content-Disposition","attachment;filename="+fileName+".xls");
        OutputStream os = response.getOutputStream();
        workbook.write(os);
        os.flush();
        os.close();
        workbook.close();
    


然后controller层的代码:

import com.example.demo.utils.PoiExcel;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;

/**
 * @author huao
 * @Date 2022/8/31 13:56
 * @description:
 */
@RestController
@RequestMapping("/demo")
public class DemoWeb 

    @RequestMapping("/download")
    public void download(我应该在 tableView(_:cellForRowAtIndexPath:) 方法中为自定义单元格写啥?

POI导出Excel表格(多行表头合并单元格)

java poi 导出 excel时 ,合并单元格的问题

用java poi包读取Excel单元格

java poi 单元格合并

请教JAVA使用POI导出excel处理空白单元格的问题