POI创建比较复杂的excel表格,明确创建行,列,合并单元格

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFDataFormat;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.CellRangeAddress;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;

public class POIExcelTest {
	 public static void main(String[] args) throws Exception {  
	        OutputStream os = new FileOutputStream("F:\\test.xls");  
	        Workbook wb = new HSSFWorkbook();  
	        POIExcelTest test = new POIExcelTest();  
	        test.createFile(os, wb);  
	    }  
	      
	    private void createFile(OutputStream os,Workbook wb) throws IOException{  
	        int i = 0;  
	        int j = 0;  
	        double trans_amt = 0.00;  
	        double ref_amt = 0.00;  
	        String[] refundLogs = new String[2];  
	        String str1 = "20110812|34234234242432|345.00|323.00";  
	        String str2 = "20110504|45656464535345|231.34|231.34";  
	        refundLogs[0] = str1;  
	        refundLogs[1] = str2;  
	        Sheet sheet = wb.createSheet("T建行退款文件");  
	        Row row = sheet.createRow(0);  
	        for(i=1;i<=3;i++){  
	            sheet.createRow(i);  
	        }  
	          
	        for(i=0;i<4;i++)  
	            row.createCell(i);  
	          
	        sheet.addMergedRegion(new CellRangeAddress(0, 3, 0, 3));  
	        //CellRangeAddress(int firstRow, int lastRow, int firstCol, int lastCol)
	        //参数:起始行号,终止行号, 起始列号,终止列号
	        Font font = wb.createFont();  
	        font.setFontName("黑体");  
	        font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);  
	          
	        CellStyle cs1 = wb.createCellStyle();  
	        cs1.setAlignment(CellStyle.ALIGN_CENTER);  
	        cs1.setDataFormat(wb.createDataFormat().getFormat("yyyyMMdd"));  
	        cs1.setFont(font);  
	          
	        CellStyle cs2 = wb.createCellStyle();  
	        cs2.setAlignment(CellStyle.ALIGN_CENTER);  
	        cs2.setDataFormat(HSSFDataFormat.getBuiltinFormat("0.00"));  
	        cs2.setFont(font);  
	          
	        CellStyle cs3 = wb.createCellStyle();  
	        cs3.setAlignment(CellStyle.ALIGN_CENTER);  
	        cs3.setFont(font);  
	          
	        row = sheet.getRow(0);  
	        Cell cell = row.getCell(0);  
	        cell.setCellType(HSSFCell.CELL_TYPE_STRING);  
	        cell.setCellValue("建行运行中心:\n\t"+"现有"+refundLogs.length+"表退款交易,请配合汇付天下公司进行审核");  
	          
	        sheet.createRow(4);  
	        row = sheet.createRow(5);  
	          
	        cell = row.createCell(0);  
	        cell.setCellType(HSSFCell.CELL_TYPE_STRING);  
	        cell.setCellValue("商户编号:");  
	        cell = row.createCell(1);  
	        cell.setCellType(HSSFCell.CELL_TYPE_STRING);  
	        cell.setCellValue("45433242");  
	          
	        row = sheet.createRow(6);  
	        cell = row.createCell(0);  
	        cell.setCellType(HSSFCell.CELL_TYPE_STRING);  
	        cell.setCellValue("交易明细:");  
	          
	        row = sheet.createRow(7);  
	        row.createCell(0).setCellValue("退款日期");  
	        row.createCell(1).setCellValue("消费卡号");  
	        row.createCell(2).setCellValue("消费金额");  
	        row.createCell(3).setCellValue("退款金额");  
	        for(i=0;i<4;i++)  
	            row.getCell(i).setCellStyle(cs3);  
	          
	          
	        for(i=8;i<=7+refundLogs.length;i++)  
	        {  
	            sheet.createRow(i);  
	            for(j=0;j<4;j++)  
	                sheet.getRow(i).createCell(j);  
	        }  
	        for(i=0;i<refundLogs.length;i++){  
	            row = sheet.getRow(8+i);  
	            String[] refundLog = refundLogs[i].split("\\|");  
	            cell = row.getCell(0);  
	            cell.setCellStyle(cs1);  
	            cell.setCellValue(refundLog[0]);  
	              
	            cell = row.getCell(1);  
	            cell.setCellType(HSSFCell.CELL_TYPE_STRING);  
	            cell.setCellStyle(cs3);  
	            cell.setCellValue(refundLog[1]);  
	              
	            cell = row.getCell(2);  
	            cell.setCellStyle(cs2);  
	            cell.setCellValue(refundLog[2]);  
	            trans_amt += Double.parseDouble(refundLog[2]);  
	              
	            cell = row.getCell(3);  
	            cell.setCellType(HSSFCell.CELL_TYPE_NUMERIC);  
	            cell.setCellStyle(cs2);  
	            cell.setCellValue(refundLog[3]);  
	            ref_amt += Double.parseDouble(refundLog[3]);  
	        }  
	          
	        row = sheet.createRow(9+i);  
	        for(i=0;i<4;i++)  
	            row.createCell(i);  
	        row.getCell(0).setCellValue("总计:");  
	        row.getCell(2).setCellValue(trans_amt);  
	        row.getCell(3).setCellValue(ref_amt);  
	          
	        sheet.autoSizeColumn(0);  
	        sheet.autoSizeColumn(1);  
	        sheet.autoSizeColumn(2);  
	        sheet.autoSizeColumn(3);  
	          
	        wb.write(os);  
	    }  
	}  

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
可以使用以下代码创建一个64表格,并将第二到第五的第一单元格合并为一个单元格: ```java import java.io.FileOutputStream; import org.apache.poi.xwpf.usermodel.*; public class TableExample { public static void main(String[] args) throws Exception { XWPFDocument doc = new XWPFDocument(); // 创建一个64表格 int rows = 6; int cols = 4; XWPFTable table = doc.createTable(rows, cols); // 设置表格样式 table.getCTTbl().addNewTblGrid().addNewGridCol().setW(BigInteger.valueOf(1500)); table.getCTTbl().getTblGrid().addNewGridCol().setW(BigInteger.valueOf(1500)); table.getCTTbl().getTblGrid().addNewGridCol().setW(BigInteger.valueOf(1500)); table.getCTTbl().getTblGrid().addNewGridCol().setW(BigInteger.valueOf(1500)); // 设置表格内容 for (int i = 0; i < rows; i++) { XWPFTableRow row = table.getRow(i); for (int j = 0; j < cols; j++) { XWPFTableCell cell = row.getCell(j); cell.setText("Row " + (i+1) + ", Col " + (j+1)); } } // 合并第二到第五的第一单元格 table.getRow(1).getCell(0).getCTTc().addNewTcPr().addNewVMerge().setVal(STMerge.CONTINUE); for (int i = 2; i <= 4; i++) { table.getRow(i).getCell(0).getCTTc().addNewTcPr().addNewVMerge().setVal(STMerge.RESTART); } // 将文档写入到文件中 FileOutputStream out = new FileOutputStream("table.docx"); doc.write(out); out.close(); System.out.println("表格生成成功!"); } } ``` 在这个示例中,我们首先创建了一个包含64表格,并设置了每一的宽度为1500。接下来,我们使用嵌套的循环来遍历每个单元格,并设置单元格的文本。然后,我们使用CTTc对象来合并第二到第五的第一单元格。具体来说,我们使用getRow()方法获取第二到第五对象,然后使用getCell()方法获取第一的单元格对象。接着,我们使用getCTTc()方法获取单元格的CTTc对象,然后使用addNewTcPr()方法获取CTTc对象的TcPr对象,并使用addNewVMerge()方法获取TcPr对象的VMerge对象。对于第二的单元格,我们将VMerge对象的val属性设置为STMerge.CONTINUE,表示该单元格是一个合并单元格的延续;对于第三到第五的单元格,我们将VMerge对象的val属性设置为STMerge.RESTART,表示该单元格是一个新的合并单元格的起始位置。最后,我们将文档写入到名为“table.docx”的文件中。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值