java 编辑excel文件合并单元格边框显示问题

现象:

第12行 边框显示不完整。

解决方法:

误区以为不用设置跨掉的单元格 ,第9行是完整单元格,第11行是画了一个格子。

如图:

 将单元格数量补全后 设置边框样式整个边框就完整了。

实现完整代码:

引入maven:

		<dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
             <version>4.1.1</version>
        </dependency>
		<dependency>
			<groupId>org.apache.poi</groupId>
			<artifactId>poi-ooxml</artifactId>
			  <version>4.1.1</version>
		</dependency>

代码:

  可以直接拷贝运行起来。

package com.weighbridge.test;

import java.io.FileOutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
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.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.FillPatternType;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.VerticalAlignment;
import org.apache.poi.ss.util.CellRangeAddress;
import org.springframework.util.StringUtils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;

public class test {

	public static void main(String[] args) {
		createdExcel();

	}
	  
	
	
	
	static String format(Object oo){
		String object = "";
		if (!StringUtils.isEmpty(oo)) {
			 object =   oo.toString();
		} 
		return object;
	}
	 

	// 初始格子
	public static void createdExcel() {

		try {
			// 创建工作薄对象
			HSSFWorkbook workbook = new HSSFWorkbook();// 这里也可以设置sheet的Name
			// 创建工作表对象
			HSSFSheet sheet = workbook.createSheet();
			
			int rowNum = 0;
			//  跨行参数从0开始       行开始   行结束     列开始  列结束
			sheet.addMergedRegion(new CellRangeAddress(rowNum, rowNum, 0,7));
			Row titleRow = sheet.createRow(rowNum++);
            titleRow.setHeightInPoints(30);
            Cell titleCell = titleRow.createCell(0);
            // 设置样式
            titleCell.setCellStyle(getStyleTitle(workbook));
            titleCell.setCellValue("统计报表");

            
            int height = 20 ;
            // 负责人  TODO
            int  i=rowNum++;
			HSSFRow row11=sheet.createRow(i);   //第三行
			sheet.addMergedRegion(new CellRangeAddress( i,  i,  0,  7 ));   //第三行的 第0列到第7列 合并单元格
			row11.setHeightInPoints(height);
			
			row11.createCell(0).setCellValue("交货单位:一车间                                                                                       2021年03月09日                                                                                               单位:kg、%vol							"); //赋值
			row11.createCell(1); //赋值
			row11.createCell(2);
			row11.createCell(3);
			row11.createCell(4);
			row11.createCell(5);
			row11.createCell(6);
			row11.createCell(7);
			for (Cell cell : row11) {
			cell.setCellStyle(getStyleCell(workbook));
			}
            
        
			
		HSSFCellStyle styleCell = getStyleHeadCell(workbook);
	    	// 设置列宽 TODO
    	  sheet.setColumnWidth(0, 5500);
    	  sheet.setColumnWidth(1, 5500);
    	  sheet.setColumnWidth(2, 5500);
    	  sheet.setColumnWidth(3, 5500);
    	  sheet.setColumnWidth(4, 5500);
    	  sheet.setColumnWidth(5, 5500);
    	  sheet.setColumnWidth(6, 5500);
    	  sheet.setColumnWidth(7, 5500);
	    	 
			// 创建工作表的行
			HSSFRow row = sheet.createRow(rowNum++);// 设置第一行,从零开始
			row.setHeightInPoints(height);
			int is = 0 ; 
			HSSFCell createCell1 = row.createCell(is++);
			createCell1.setCellStyle(styleCell);
			createCell1.setCellValue("单位");// 第一行单位
			
			createCell1 = row.createCell(is++);
			createCell1.setCellStyle(styleCell);
			createCell1.setCellValue("入库数");// 第一行 入库度大
	    	
			createCell1 = row.createCell(is++);
			createCell1.setCellStyle(styleCell);
			createCell1.setCellValue("二入库数");// 第一行 入库度二
			
			createCell1 = row.createCell(is++);
			createCell1.setCellStyle(styleCell);
			createCell1.setCellValue("合计头");// 第一行合计头
			
			createCell1 = row.createCell(is++);
			createCell1.setCellStyle(styleCell);
			createCell1.setCellValue("入库量");// 第一行入库量大
			
			createCell1 = row.createCell(is++);
			createCell1.setCellStyle(styleCell);
			createCell1.setCellValue("入库量二");// 第一行入库量二
	    	
			createCell1 = row.createCell(is++);
			createCell1.setCellStyle(styleCell);
			createCell1.setCellValue("合计");// 第一行合计
			
			createCell1 = row.createCell(is++);
			createCell1.setCellStyle(styleCell);
			createCell1.setCellValue("扣除损耗合计");// 第一行扣除损耗合计
	    	
	    	
			HSSFRow row1 = sheet.createRow(rowNum++);// 设置第二行,从零开始
			row1.setHeightInPoints(height);
			styleCell = getStyleCell(workbook);
		    String ss = "[ 	{ 		\"departmentName\": \"一组\", 		\"hj\": 40.0, 		\"drclljshh\": 0.0, 		\"alcoholContent2\": 0.0, 		\"alcoholContent1\": 65.0, 		\"jdbdblj\": 0.0, 		\"optCounter\": 0, 		\"updatedDate\": 1634199611860, 		\"delFlag\": 0, 		\"clljshq\": 0.0, 		\"jubdbgs\": 0, 		\"kcshhj\": 39.85, 		\"createdDate\": 1633934873000, 		\"clljshh\": 0.0, 		\"headWeight\": 5.0, 		\"drjdbdblj\": 0.0, 		\"jtbdbgs\": 0, 		\"departmentNo\": \"01010\", 		\"netWeight1\": 35.0, 		\"id\": \"0a5d9f2753f34a698f90158f3b2c223a\", 		\"netWeight2\": 0.0, 		\"drclljshq\": 0.0 	} ]";
		    JSONArray parseArray = JSON.parseArray(ss);
		    for (Object object : parseArray) {
		    	Map<String, Object>  map =  (Map<String, Object>) object;
		    	i = 0 ; 
		    	HSSFCell createCell2 = row1.createCell(i++);
				createCell2.setCellStyle(styleCell);
				createCell2.setCellValue(format(map.get("departmentName")));// 第一行单位
				
				createCell2 = row1.createCell(i++);
				createCell2.setCellStyle(styleCell);
				createCell2.setCellValue(format(map.get("alcoholContent1")));//第一行 入库度大
				
				createCell2 = row1.createCell(i++);
				createCell2.setCellStyle(styleCell);
				createCell2.setCellValue(format(map.get("alcoholContent2")));// 第一行 入库度二
				
				createCell2 = row1.createCell(i++);
				createCell2.setCellStyle(styleCell);
				createCell2.setCellValue(format(map.get("headWeight")));// 第一行合计头
				
				createCell2 = row1.createCell(i++);
				createCell2.setCellStyle(styleCell);
				createCell2.setCellValue(format(map.get("netWeight1")));//  第一行入库量大
				
				createCell2 = row1.createCell(i++);
				createCell2.setCellStyle(styleCell);
				createCell2.setCellValue(format(map.get("netWeight2")));// 第一行入库量二
				
				createCell2 = row1.createCell(i++);
				createCell2.setCellStyle(styleCell);
				createCell2.setCellValue(format(map.get("hj")));// 第一行合计
				
				createCell2 = row1.createCell(i++);
				createCell2.setCellStyle(styleCell);
				createCell2.setCellValue(format(map.get("kcshhj")));// 第一行扣除损耗合计
				
			} 
		    //		    备注  合计 
		    i = rowNum++;
		    HSSFRow rowRemake = sheet.createRow(i);
		    rowRemake.setHeightInPoints(height);
			//  跨行参数从0开始       行开始   行结束     列开始  列结束
			sheet.addMergedRegion(new CellRangeAddress(i, i, 0,1));
			 
			rowRemake.createCell(0).setCellValue("备注:各组各扣除0.15的损耗量"); //赋值
			rowRemake.createCell(1); //赋值
			rowRemake.createCell(2).setCellValue("合计"); 
			rowRemake.createCell(3).setCellValue("2"); 
			rowRemake.createCell(4).setCellValue("2"); 
			rowRemake.createCell(5).setCellValue("2"); 
			rowRemake.createCell(6).setCellValue("2"); 
			rowRemake.createCell(7).setCellValue("2"); 
			for (Cell cell : rowRemake) {
			cell.setCellStyle(getStyleCell(workbook));
			}
			
			
		    
	      // 负责人  TODO
        i=rowNum++;
		row11=sheet.createRow(i);   //第三行
		row11.setHeightInPoints(height);
		sheet.addMergedRegion(new CellRangeAddress( i,  i,  0,  7 ));   //第三行的 第0列到第7列 合并单元格
		 
		row11.createCell(0).setCellValue("负责人:                                                                                                    统计员:                                                                                                     报出日期:							"); //赋值
		row11.createCell(1); //赋值
		row11.createCell(2);
		row11.createCell(3);
		row11.createCell(4);
		row11.createCell(5);
		row11.createCell(6);
		row11.createCell(7);
		for (Cell cell : row11) {
		cell.setCellStyle(getStyleCell(workbook));
		}
	 
		
		
		    height = 20 ;
	        // 负责人  TODO
	        i=9;  // 9 行
			row11=sheet.createRow(i);   //第三行
			sheet.addMergedRegion(new CellRangeAddress( i,  i,  0,  7 ));   //第三行的 第0列到第7列 合并单元格
			row11.setHeightInPoints(height);
			
			row11.createCell(0).setCellValue("测试跨行"); //赋值
			row11.createCell(1); //赋值
			row11.createCell(2);
			row11.createCell(3);
			row11.createCell(4);
			row11.createCell(5);
			row11.createCell(6);
			row11.createCell(7);
			for (Cell cell : row11) {
			cell.setCellStyle(getStyleCell(workbook));
			}
        
		
		
		  	height = 20 ;
	        // 负责人  TODO
	        i=11;   // 11 行
			row11=sheet.createRow(i);   //第三行
			sheet.addMergedRegion(new CellRangeAddress( i,  i,  0,  7 ));   //第三行的 第0列到第7列 合并单元格
			row11.setHeightInPoints(height);
			
			row11.createCell(0).setCellValue("测试跨行2"); //赋值
//			row11.createCell(1); //赋值
//			row11.createCell(2);
//			row11.createCell(3);
//			row11.createCell(4);
//			row11.createCell(5);
//			row11.createCell(6);
//			row11.createCell(7);
			for (Cell cell : row11) {
			cell.setCellStyle(getStyleCell(workbook));
			}
	        
		 
			
			workbook.setSheetName(0, "sheetName");// 设置sheet的Name

			// 文档输出d
			FileOutputStream out = new FileOutputStream(
					"C:\\Users\\admin\\Desktop\\test\\"
							+ new SimpleDateFormat("yyyyMMddHHmmss").format(
									new Date()).toString() + ".xls");
			workbook.write(out);
			out.close();

		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	 


	/*  
	 * 标题样式
	 */  
  	public static HSSFCellStyle getStyleTitle(HSSFWorkbook workbook) {
	  	  // 设置字体
	  	  HSSFFont font = workbook.createFont();
	  	  //设置字体大小
	  	  font.setFontHeightInPoints((short)15);
	  	  //字体加粗
	  	  font.setBold(true); 
	  	  //设置字体名字 
	  	  font.setFontName("Arial");
	  	  //设置样式; 
	  	  HSSFCellStyle style = workbook.createCellStyle();
	  
	  	  //在样式用应用设置的字体;  
	  	  style.setFont(font);
	  	  //设置自动换行; 
	  	  style.setWrapText(false);
	  	  //设置水平对齐的样式为居中对齐;  
	  	  style.setAlignment(HorizontalAlignment.CENTER);
	  	  //设置垂直对齐的样式为居中对齐; 
	  	  style.setVerticalAlignment(VerticalAlignment.CENTER);
	      style.setWrapText(true);
	  	  
	  	  return style;
  	
  	}

  	
  	
	/*  
	 * 列数据信息单元格样式
	 */  
  	public static HSSFCellStyle getStyleCell(HSSFWorkbook workbook) {
	  	  // 设置字体
	  	  HSSFFont font = workbook.createFont();
	  	  //设置字体大小
	  	  font.setFontHeightInPoints((short)12);
	  	  //字体加粗
//	  	  font.setBold(true); 
	  	  //设置字体名字 
	  	  font.setFontName("Arial");
	  	  //设置样式; 
	  	  HSSFCellStyle style = workbook.createCellStyle();
	  
	  	  //在样式用应用设置的字体;  
	  	  style.setFont(font);
	  	  //设置自动换行; 
	  	  style.setWrapText(false);
	  	  //设置水平对齐的样式为居中对齐;  
	  	  style.setAlignment(HorizontalAlignment.CENTER);
	  	  //设置垂直对齐的样式为居中对齐; 
	  	  style.setVerticalAlignment(VerticalAlignment.CENTER);
	  	  
	  	  
		  //设置样式对象,这里仅设置了边框属性
		  style.setBorderBottom(BorderStyle.THIN); //下边框
		  style.setBorderLeft(BorderStyle.THIN);//左边框
		  style.setBorderTop(BorderStyle.THIN);//上边框
		  style.setBorderRight(BorderStyle.THIN);//右边框
	  	  
	  
	  	  return style;
  	
  	}
	
  	
  	/*  
  	 * 头列数据信息单元格样式
  	 */  
  	public static HSSFCellStyle getStyleHeadCell(HSSFWorkbook workbook) {
  	  //设置样式; 
	  	 HSSFCellStyle style = workbook.createCellStyle();
	  
  		 style = workbook.createCellStyle();
         style.setAlignment(HorizontalAlignment.CENTER);
         style.setFillForegroundColor(IndexedColors.GREY_50_PERCENT.getIndex());
         style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
         Font headerFont = workbook.createFont();
         headerFont.setFontName("Arial");
         headerFont.setFontHeightInPoints((short) 10);
         headerFont.setBold(true);
         headerFont.setColor(IndexedColors.WHITE.getIndex());
         style.setFont(headerFont);
         //设置垂直对齐的样式为居中对齐; 
	  	  style.setVerticalAlignment(VerticalAlignment.CENTER);
  		return style;
  		
  	}
  	
	
}

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 在Java中,要导出带有合并单元Excel文件,可以使用Apache POI库。Apache POI是一个用于操作各种Microsoft Office文件Java库。以下是一个简单的示例代码来导出带有合并单元Excel文件: ``` import org.apache.poi.ss.usermodel.*; import org.apache.poi.xssf.usermodel.*; import java.io.FileOutputStream; import java.io.IOException; public class ExcelExportExample { public static void main(String[] args) { String[] headers = {"姓名", "学科", "分数"}; String[][] data = {{"小明", "数学", "90"}, {"小明", "英语", "80"}, {"小明", "语文", "95"}}; try (Workbook workbook = new XSSFWorkbook()) { Sheet sheet = workbook.createSheet("成绩表"); Row headerRow = sheet.createRow(0); for (int i = 0; i < headers.length; i++) { Cell cell = headerRow.createCell(i); cell.setCellValue(headers[i]); } for (int i = 0; i < data.length; i++) { Row dataRow = sheet.createRow(i + 1); for (int j = 0; j < data[i].length; j++) { Cell cell = dataRow.createCell(j); cell.setCellValue(data[i][j]); } } // 合并单元 CellRangeAddress region = new CellRangeAddress(1, 1, 0, 2); sheet.addMergedRegion(region); try (FileOutputStream outputStream = new FileOutputStream("成绩表.xlsx")) { workbook.write(outputStream); } System.out.println("Excel文件导出成功!"); } catch (IOException e) { e.printStackTrace(); } } } ``` 以上代码创建了一个包含姓名、学科和分数的表头和对应的数据行的Excel。然后使用`CellRangeAddress`类来指定需要合并单元,这里示例代码将第一行的第一列到第三列进行合并。最后将Workbook写入到文件输出流中,即可将带有合并单元Excel文件导出成功。 ### 回答2: 在Java中导出带有合并单元Excel文件可以通过使用Apache POI库来实现。Apache POI是一个开源的Java库,用于处理Microsoft Office文件,包括Excel。 首先,确保已经在Java项目中引入了Apache POI库的相关依赖。 接下来,我们需要创建一个Workbook对象(代表一个Excel文件),并设置合并单元的样式。可以使用CellStyle对象来定义合并单元的样式,例如设置背景颜色、边框等。 然后,创建一个Sheet对象(代表一个Excel工作表),并在其中设置合并单元的区域。可以使用Sheet对象的addMergedRegion方法来实现,指定需要合并单元区域的起始行、起始列、结束行、结束列。 最后,通过创建Row和Cell对象,并设置其值,将数据写入合并单元的位置。 下面是一个简单的示例代码: ```java import org.apache.poi.ss.usermodel.*; public class ExcelExporter { public static void main(String[] args) { Workbook workbook = new XSSFWorkbook(); Sheet sheet = workbook.createSheet("Sheet1"); CellStyle style = workbook.createCellStyle(); style.setFillForegroundColor(IndexedColors.YELLOW.getIndex()); style.setFillPattern(FillPatternType.SOLID_FOREGROUND); style.setBorderBottom(BorderStyle.THIN); style.setBorderTop(BorderStyle.THIN); style.setBorderLeft(BorderStyle.THIN); style.setBorderRight(BorderStyle.THIN); sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 2)); // 合并第一行的前三列 Row row = sheet.createRow(0); Cell cell = row.createCell(0); cell.setCellValue("合并单元"); cell.setCellStyle(style); // 写入文件或进行其他操作 } } ``` 以上代码创建了一个包含合并单元Excel文件合并了第一行的前三列,并将"合并单元"写入合并单元的位置。可以根据实际需求进行修改和扩展。 希望对你有所帮助! ### 回答3: 在Java中导出带有合并单元Excel可以使用Apache POI库来实现。下面是一个简单的示例代码: ```java import org.apache.poi.ss.usermodel.*; import org.apache.poi.ss.util.CellRangeAddress; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import java.io.FileOutputStream; import java.io.IOException; public class ExcelExportDemo { public static void main(String[] args) { // 创建工作簿 Workbook workbook = new XSSFWorkbook(); // 创建工作表 Sheet sheet = workbook.createSheet("Sheet1"); // 创建合并单元区域 CellRangeAddress mergeRegion = new CellRangeAddress(0, 0, 0, 3); // 合并单元 sheet.addMergedRegion(mergeRegion); // 创建行 Row row = sheet.createRow(0); // 创建单元 Cell cell = row.createCell(0); // 设置单元内容 cell.setCellValue("合并单元示例"); // 导出Excel文件 try { FileOutputStream outputStream = new FileOutputStream("output.xlsx"); workbook.write(outputStream); outputStream.flush(); outputStream.close(); System.out.println("Excel文件导出成功!"); } catch (IOException e) { e.printStackTrace(); } } } ``` 以上代码创建了一个工作簿和工作表,然后设置合并单元的区域,创建行和单元,并设置单元的值为"合并单元示例"。最后将工作簿导出为Excel文件。在Excel中打开导出的文件,可以看到第一行的前四列单元合并为一个单元,并且单元的内容为"合并单元示例"。 注意:在运行代码之前,确保已经导入了Apache POI库的相关依赖。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值