poi操作Excel(合并)
来源:互联网 发布:vc连接access数据库 编辑:程序博客网 时间:2024/06/07 05:57
效果图(稍微不是很美观)
简单实现的代码:
package com.ucsoft.bas.task.web.order;import java.io.FileOutputStream;import org.apache.poi.hssf.usermodel.HSSFWorkbook;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;import org.apache.poi.ss.util.CellRangeAddress;/** * poi操作Excel(合并) * * @author hgg * @version 2015年2月14日上午10:27:23 */public class PoiExcel {public static void main(String[] args) {// 创建一个新的excelWorkbook wb = new HSSFWorkbook();// 创建sheet页Sheet sheet = wb.createSheet("new sheet");// 设置样式CellStyle cellStyle = wb.createCellStyle();// 对齐方式cellStyle.setAlignment(CellStyle.ALIGN_LEFT);cellStyle.setVerticalAlignment(CellStyle.VERTICAL_CENTER);// 边框cellStyle.setBorderLeft(CellStyle.BORDER_THIN);cellStyle.setBorderRight(CellStyle.BORDER_THIN);cellStyle.setBorderTop(CellStyle.BORDER_THIN);cellStyle.setBorderBottom(CellStyle.BORDER_THIN);cellStyle.setWrapText(true);// 自动换行cellStyle.setFont(setFont(wb));// 设置字体Row row = sheet.createRow((short) 0);Cell cell = row.createCell((short) 0);cell.setCellStyle(cellStyle);cell.setCellValue("合并列");// 合并列sheet.addMergedRegion(new CellRangeAddress(0, // first row (0-based)0, // last row (0-based)0, // first column (0-based)1 // last column (0-based)));row = sheet.createRow((short) 1);cell = row.createCell((short) 0);cell.setCellStyle(cellStyle);cell.setCellValue("合并行");// 合并行sheet.addMergedRegion(new CellRangeAddress(1, 4, 0, 0));// 合并后的行1cell = row.createCell((short) 1);cell.setCellStyle(cellStyle);cell.setCellValue("行2-1");cell = row.createCell((short) 2);cell.setCellStyle(cellStyle);cell.setCellValue("行2-2");// 合并后的行2row = sheet.getRow(2);if (row == null) {row = sheet.createRow(2);}cell = row.createCell((short) 1);cell.setCellStyle(cellStyle);cell.setCellValue("3-1");cell = row.createCell((short) 2);cell.setCellStyle(cellStyle);cell.setCellValue("3-2");// 合并后的行3row = sheet.getRow(3);if (row == null) {row = sheet.createRow(3);}cell = row.createCell((short) 1);cell.setCellStyle(cellStyle);cell.setCellValue("4-1");cell = row.createCell((short) 2);cell.setCellStyle(cellStyle);cell.setCellValue("4-2");// 单独的一行row = sheet.createRow(5);cell = row.createCell(0);cell.setCellStyle(cellStyle);cell.setCellValue("单独的一行(第五行)");// Write the output to a fileFileOutputStream fileOut;try {fileOut = new FileOutputStream("c:/workbook.xls");wb.write(fileOut);fileOut.close();} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}}/** * 设置字体 * * @param wb * @return * @author hgg * @version 2015年2月14日上午10:47:50 */private static Font setFont(Workbook wb) {// Create a new font and alter it.Font font = wb.createFont();font.setFontHeightInPoints((short) 12);font.setFontName("Courier New");// font.setItalic(true);// font.setStrikeout(true);//删除线return font;}}
0 0
- poi操作Excel(合并)
- JAVA POI 操作EXCEL 合并单元格
- poi操作excel(主要为合并单元格和导出excel)
- poi excel合并单元格
- poi excel单元格合并
- POI实战-java开发excel详解(第四章 常用操作-单元格合并与数据读取)
- POI操作Excel(转)
- poi操作excel(二)
- java:Excel操作(poi)
- poi 操作excel , 合并单元格后加边框
- poi操作excel(下载excel)
- POI导出Excel--合并单元格
- POI之Excel单元格合并
- POI导出Excel 合并单元格
- POI导出excel合并单元格
- Apache POI Excel合并单元格
- POI导出Excel--合并单元格
- 一脸懵逼学习Java操作Excel之POI(Apache POI)
- 13.HCNA-HNTD——交换网络基础
- Python标准库:内置函数pow(x, y[, z])
- 2015节后第一天
- 实用的Scala泛函编程
- Hadoop极速体验
- poi操作Excel(合并)
- oracle 时间类型处理
- 怎样学习Scala泛函编程
- PostgreSQL do language plpgsql $$ ...
- 测试RSS订阅,稍后删除
- JAVA学习笔记47——其他容器简介4:guava之MulitiSet、MultiMap、BiMap、Table
- 第六章 HASH
- SQL练习题
- 开源 java CMS - FreeCMS2.3发布