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
原创粉丝点击