zoukankan      html  css  js  c++  java
  • poi

    POI中可能会用到一些需要设置EXCEL单元格格式的操作小结: 
    先获取工作薄对象:
    
    Java代码
    
    HSSFWorkbook wb = new HSSFWorkbook(); 
    HSSFSheet sheet = wb.createSheet(); 
    HSSFCellStyle setBorder = wb.createCellStyle();
    一、设置背景色:
    
    Java代码
    
    setBorder.setFillForegroundColor((short) 13);// 设置背景色
    setBorder.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
    二、设置边框:
    
    Java代码
    
    setBorder.setBorderBottom(HSSFCellStyle.BORDER_THIN); //下边框
    setBorder.setBorderLeft(HSSFCellStyle.BORDER_THIN);//左边框
    setBorder.setBorderTop(HSSFCellStyle.BORDER_THIN);//上边框
    setBorder.setBorderRight(HSSFCellStyle.BORDER_THIN);//右边框
    三、设置居中:
    
    Java代码
    
    setBorder.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 居中
    四、设置字体:
    
    Java代码
    
    HSSFFont font = wb.createFont(); 
    font.setFontName("黑体"); 
    font.setFontHeightInPoints((short) 16);//设置字体大小
    HSSFFont font2 = wb.createFont(); 
    font2.setFontName("仿宋_GB2312"); 
    font2.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);//粗体显示
    font2.setFontHeightInPoints((short) 12); 
    setBorder.setFont(font);//选择需要用到的字体格式
    五、设置列宽:
    
    Java代码
    
    sheet.setColumnWidth(0, 3766); //第一个参数代表列id(从0开始),第2个参数代表宽度值
    六、设置自动换行:
    
    Java代码
    
    setBorder.setWrapText(true);//设置自动换行
    七、合并单元格:
    
    Java代码
    
    Region region1 = new Region(0, (short) 0, 0, (short) 6); 
    //参数1:行号 参数2:起始列号 参数3:行号 参数4:终止列号
    sheet.addMergedRegion(region1);
    View Code
    + View code
    package cn.com.util;  
    
    import org.apache.poi.hssf.usermodel.HSSFCellStyle;  
    import org.apache.poi.hssf.usermodel.HSSFFont;  
    import org.apache.poi.hssf.usermodel.HSSFRichTextString;  
    import org.apache.poi.hssf.usermodel.HSSFWorkbook;  
    import org.apache.poi.hssf.usermodel.HSSFSheet;  
    import org.apache.poi.hssf.usermodel.HSSFRow;  
    import org.apache.poi.hssf.usermodel.HSSFCell;  
    import org.apache.poi.hssf.util.CellRangeAddress;  
    import org.apache.poi.hssf.util.Region;  
    import org.apache.poi.ss.usermodel.CellStyle;  
    
    import java.io.FileOutputStream;  
    
    import javax.servlet.http.HttpServlet;  
    
    public class CreateXL extends HttpServlet {  
    
    public static String outputFile = "c:\test.xls";  
    
    private void cteateCell(HSSFWorkbook wb, HSSFRow row, short col, String val) {  
    HSSFCell cell = row.createCell(col);  
    // cell.setEncoding(HSSFCell.ENCODING_UTF_16);
    cell.setCellValue(val);  
    HSSFCellStyle cellstyle = wb.createCellStyle();  
    cellstyle.setAlignment(HSSFCellStyle.ALIGN_CENTER_SELECTION);  
    cell.setCellStyle(cellstyle);  
    }  
    
    public static void main(String argv[]) {  
    try {  
    // 创建新的Excel 工作簿
    HSSFWorkbook workbook = new HSSFWorkbook();  
    
    // 设置字体
    HSSFFont font = workbook.createFont();  
    // font.setColor(HSSFFont.COLOR_RED);
    font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);  
    font.setFontHeightInPoints((short) 14);  
    
    // HSSFFont font2 = workbook.createFont();
    // font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
    // font.setFontHeightInPoints((short)14);
    
    // 设置样式
    HSSFCellStyle cellStyle = workbook.createCellStyle();  
    cellStyle.setFont(font);  
    cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);  
    
    // HSSFCellStyle cellStyle2= workbook.createCellStyle();
    // cellStyle.setFont(font2);
    // cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
    
    // 在Excel工作簿中建一工作表,其名为缺省值
    // 如要新建一名为"月报表"的工作表,其语句为:
    HSSFSheet sheet = workbook.createSheet("月报表");  
    CellRangeAddress cellRangeAddress = new CellRangeAddress(0, 0, 0,  
    11);  
    sheet.addMergedRegion(cellRangeAddress);  
    
    //第一行
    // 在索引0的位置创建行(最顶端的行)
    HSSFRow row = sheet.createRow(0);  
    // 在索引0的位置创建单元格(左上端)
    HSSFCell cell = row.createCell(0);  
    // 定义单元格为字符串类型
    cell.setCellType(HSSFCell.CELL_TYPE_STRING);  
    cell.setCellStyle(cellStyle);  
    // 在单元格中输入一些内容
    cell.setCellValue(new HSSFRichTextString("北京亿卡联科技发展有限公司小区门禁维修月报表"));  
    
    //第二行
    cellRangeAddress = new CellRangeAddress(1, 1, 3, 6);  
    sheet.addMergedRegion(cellRangeAddress);  
    row = sheet.createRow(1);  
    HSSFCell datecell = row.createCell(3);  
    datecell.setCellType(HSSFCell.CELL_TYPE_STRING);  
    datecell.setCellStyle(cellStyle);  
    datecell.setCellValue("时间间隔xxxxx");  
    
    cellRangeAddress = new CellRangeAddress(1, 1, 9,  
    10);  
    sheet.addMergedRegion(cellRangeAddress);  
    row.createCell(9).setCellValue("单位:元");  
    
    //第三行
    row=sheet.createRow(2);  
    row.createCell(0).setCellValue("一、");  
    row.createCell(1).setCellValue("基本资料");  
    
    //第4行
    row=sheet.createRow(3);  
    row.createCell(1).setCellValue("小区名称:");  
    cellRangeAddress=new CellRangeAddress(3,3,2,11);  
    sheet.addMergedRegion(cellRangeAddress);  
    row.createCell(2).setCellValue("xxxxx");  
    
    //第5行
    row=sheet.createRow(4);  
    row.createCell(1).setCellValue("座落地点:");  
    cellRangeAddress=new CellRangeAddress(4,4,2,11);  
    sheet.addMergedRegion(cellRangeAddress);  
    row.createCell(2).setCellValue("xxxxx");  
    
    //第6行
    row=sheet.createRow(5);  
    row.createCell(1).setCellValue("建成年月:");  
    cellRangeAddress=new CellRangeAddress(5,5,2,4);  
    sheet.addMergedRegion(cellRangeAddress);  
    row.createCell(2).setCellValue("年月日:xxxxx");  
    row.createCell(5).setCellValue("联系人");  
    cellRangeAddress=new CellRangeAddress(5,5,6,8);  
    sheet.addMergedRegion(cellRangeAddress);  
    row.createCell(6).setCellValue("XXX");  
    row.createCell(9).setCellValue("电话");  
    cellRangeAddress=new CellRangeAddress(5,5,10,11);  
    sheet.addMergedRegion(cellRangeAddress);  
    row.createCell(10).setCellValue("XXX");  
    
    //第7行
    row=sheet.createRow(6);  
    row.createCell(1).setCellValue("住户:");  
    row.createCell(2).setCellValue("(XX)");  
    row.createCell(3).setCellValue("(户)");  
    cellRangeAddress=new CellRangeAddress(6,6,4,5);  
    sheet.addMergedRegion(cellRangeAddress);  
    row.createCell(4).setCellValue("共计(  )"); 
    row.createCell(6).setCellValue("幢");  
    cellRangeAddress=new CellRangeAddress(6,6,7,8);  
    sheet.addMergedRegion(cellRangeAddress);  
    row.createCell(7).setCellValue("发卡张数"); 
    cellRangeAddress=new CellRangeAddress(6,6,9,10);  
    sheet.addMergedRegion(cellRangeAddress);  
    row.createCell(9).setCellValue("xxxx"); 
    
    
    //第9行
    row=sheet.createRow(8);  
    row.createCell(0).setCellValue("二、");  
    cellRangeAddress=new CellRangeAddress(8,8,1,2);  
    sheet.addMergedRegion(cellRangeAddress);  
    row.createCell(1).setCellValue("维修用材料台账");  
    row.createCell(6).setCellValue("三、");  
    cellRangeAddress=new CellRangeAddress(8,8,7,9);  
    sheet.addMergedRegion(cellRangeAddress);  
    row.createCell(7).setCellValue("维修工时记录");  
    //第10行
    row=sheet.createRow(9);  
    row.createCell(0).setCellValue("日期");  
    row.createCell(1).setCellValue("维修事项");  
    row.createCell(2).setCellValue("材料清单");  
    row.createCell(3).setCellValue("数量");  
    row.createCell(4).setCellValue("单价");  
    row.createCell(5).setCellValue("材料金额");  
    
    row.createCell(7).setCellValue("日期");  
    row.createCell(8).setCellValue("技工");  
    row.createCell(9).setCellValue("工时数");  
    row.createCell(10).setCellValue("单价");  
    row.createCell(11).setCellValue("工时金额");  
    
    //填充数据
    for (int i = 0; i < 10; i++) {  
    row=sheet.createRow(9+i+1);  
    row.createCell(0).setCellValue("日期");  
    row.createCell(1).setCellValue("维修事项");  
    row.createCell(2).setCellValue("材料清单");  
    row.createCell(3).setCellValue("数量");  
    row.createCell(4).setCellValue("单价");  
    row.createCell(5).setCellValue("材料金额");  
    
    row.createCell(7).setCellValue("日期");  
    row.createCell(8).setCellValue("技工");  
    row.createCell(9).setCellValue("工时数");  
    row.createCell(10).setCellValue("单价");  
    row.createCell(11).setCellValue("工时金额");  
    }  
    
    
    //第n+10行
    row=sheet.createRow(9+10+1);  
    //cellRangeAddress=new CellRangeAddress(19,19,0,4);
    //sheet.addMergedRegion(cellRangeAddress);
    row.createCell(0).setCellValue("累计:");  
    row.createCell(1).setCellValue("xxx");  
    row.createCell(7).setCellValue("累计:");  
    row.createCell(8).setCellValue("xxx");  
    
    
    
    
    // 新建一输出文件流
    FileOutputStream fOut = new FileOutputStream(outputFile);  
    // 把相应的Excel 工作簿存盘
    workbook.write(fOut);  
    fOut.flush();  
    // 操作结束,关闭文件
    fOut.close();  
    System.out.println("文件生成...");  
    } catch (Exception e) {  
    System.out.println("已运行 xlCreate() : " + e);  
    }  
    }  
    }
    View Code
    //定义字体  
    HSSFFont redFont = (HSSFFont) workbook.createFont();  
    redFont.setColor(HSSFColor.RED.index);// 红色  
    HSSFFont blueFont = (HSSFFont) workbook.createFont();  
    redFont.setColor(HSSFColor.BLUE.index);// 蓝色  
    //创建一个单元格  
    HSSFCell hssfCell = row.createCell(idx);  
    //创建富文本字符串,内容为:Hello,World!  
    HSSFRichTextString richString = new HSSFRichTextString( "Hello, World!" );  
    //对"Hello,"设置redFont字体  
    richString.applyFont( 0, 6, redFont );  
    //对"World!"设置blueFont字体  
    richString.applyFont( 6, 13, blueFont );  
    //将富文本字符串设置到单元格中  
    hssfCell.setCellValue( richString );  
    package com.svse.test;  
      
      
    import java.awt.Color;  
    import java.io.FileInputStream;  
    import java.io.FileNotFoundException;  
    import java.io.FileOutputStream;  
    import java.io.IOException;  
    import java.io.InputStream;  
    import java.util.Calendar;  
    import java.util.Date;  
      
      
    import org.apache.poi.hssf.usermodel.HSSFCell;  
    import org.apache.poi.hssf.usermodel.HSSFClientAnchor;  
    import org.apache.poi.hssf.usermodel.HSSFFooter;  
    import org.apache.poi.hssf.usermodel.HSSFPatriarch;  
    import org.apache.poi.hssf.usermodel.HSSFRichTextString;  
    import org.apache.poi.hssf.usermodel.HSSFShape;  
    import org.apache.poi.hssf.usermodel.HSSFSheet;  
    import org.apache.poi.hssf.usermodel.HSSFSimpleShape;  
    import org.apache.poi.hssf.usermodel.HSSFTextbox;  
    import org.apache.poi.hssf.usermodel.HSSFWorkbook;  
    import org.apache.poi.hssf.util.HSSFColor;  
    import org.apache.poi.openxml4j.exceptions.InvalidFormatException;  
    import org.apache.poi.ss.usermodel.Cell;  
    import org.apache.poi.ss.usermodel.CellStyle;  
    import org.apache.poi.ss.usermodel.ClientAnchor;  
    import org.apache.poi.ss.usermodel.CreationHelper;  
    import org.apache.poi.ss.usermodel.DataFormat;  
    import org.apache.poi.ss.usermodel.DateUtil;  
    import org.apache.poi.ss.usermodel.Drawing;  
    import org.apache.poi.ss.usermodel.Font;  
    import org.apache.poi.ss.usermodel.IndexedColors;  
    import org.apache.poi.ss.usermodel.Picture;  
    import org.apache.poi.ss.usermodel.PrintSetup;  
    import org.apache.poi.ss.usermodel.RichTextString;  
    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.usermodel.WorkbookFactory;  
    //import org.apache.poi.ss.usermodel.contrib.CellUtil;  
    //import org.apache.poi.ss.usermodel.contrib.RegionUtil;  
    import org.apache.poi.ss.util.CellRangeAddress;  
    import org.apache.poi.ss.util.CellReference;  
    import org.apache.poi.ss.util.CellUtil;  
    import org.apache.poi.ss.util.RegionUtil;  
    import org.apache.poi.util.IOUtils;  
    import org.apache.poi.xssf.usermodel.XSSFCellStyle;  
    import org.apache.poi.xssf.usermodel.XSSFWorkbook;  
    import org.junit.BeforeClass;  
    import org.junit.Test;  
      
      
    /** 
     * @author WESTDREAM 
     * @since 2010-8-7 下午10:34:03 
     */  
    public class POIExcelTest {  
      
      
        /** 
         * @throws java.lang.Exception 
         */  
        public static final String XLS_WORKBOOK_LOCATION = "D:/workbook.xls";  
        public static final String XLS_OR_XLSX_DIR = "D:/";  
        public static final String XLSX_WORKBOOK_LOCATION = "D:/workbook.xlsx";  
        public static final String IMAGE_LOCATION = "book.jpg";  
      
      
        @BeforeClass  
        public static void setUpBeforeClass() throws Exception {  
      
      
        }  
      
      
        @Test  
        public void testWriteExcel() {  
            // ## 重复利用 的对象 ##//  
            Workbook wb = null;  
            FileOutputStream fileOut = null;  
            CellStyle cellStyle = null;  
            Cell cell = null;  
            Font font = null;  
      
      
            /** 
             * EXCEL早期版本 
             */  
            try {  
                // ## 创建早期EXCEL的Workbook ##//  
                wb = new HSSFWorkbook();  
                // ## 获取HSSF和XSSF的辅助类 ##//  
                CreationHelper createHelper = wb.getCreationHelper();  
                // ## 创建一个名为“New Sheet”的Sheet ##//  
                Sheet sheet = wb.createSheet("New Sheet");  
      
      
                /** 第一行 --- CELL创建,数据填充及日期格式 **/  
                Row row1 = sheet.createRow(0);  
                // Cell cell = row.createCell(0);  
                // cell.setCellValue(1);  
      
      
                // ## 在相应的位置填充数据 ##//  
                row1.createCell(0).setCellValue(1);  
                row1.createCell(1).setCellValue(1.2);  
                row1.createCell(2).setCellValue(  
                        createHelper.createRichTextString("CreationHelper---字符串"));  
                row1.createCell(3).setCellValue(true);  
      
      
                // ## 填充日期类型的数据---未设置Cell Style ##//  
                row1.createCell(4).setCellValue(new Date());  
                // ## 填充日期类型的数据---已设置Cell Style ##//  
                cellStyle = wb.createCellStyle();  
                cellStyle.setDataFormat(createHelper.createDataFormat().getFormat(  
                        "yyyy年MM月dd日 hh:mm:ss"));  
                // cellStyle.setDataFormat(createHelper.createDataFormat().getFormat("mm/dd/yyyy h:mm"));  
                cellStyle.setDataFormat(createHelper.createDataFormat().getFormat(  
                        "yyyy-MM-dd hh:mm:ss"));  
                cell = row1.createCell(5);  
                cell.setCellValue(new Date());  
                cell.setCellStyle(cellStyle);  
                // ## 另一种创建日期的方法 ##//  
                /* 
                 * cell = row1.createCell(6); 
                 * cell.setCellValue(Calendar.getInstance()); 
                 * cell.setCellStyle(cellStyle); 
                 */  
      
      
                /** 第二行 --- 数据类型 **/  
                Row row2 = sheet.createRow(1);  
                row2.createCell(0).setCellValue(1.1);  
                row2.createCell(1).setCellValue(new Date());  
                row2.createCell(2).setCellValue(Calendar.getInstance());  
                row2.createCell(3).setCellValue("字符串");  
                row2.createCell(4).setCellValue(true);  
                // ## 错误的CELL数据格式 ##//  
                row2.createCell(5).setCellType(HSSFCell.CELL_TYPE_ERROR);  
      
      
                /** 第三行 --- CELL的各种对齐方式 **/  
                Row row3 = sheet.createRow(2);  
                row3.setHeightInPoints(30);  
                // ## 水平居中,底端对齐 ##//  
                createCell(wb, row3, (short) 0, XSSFCellStyle.ALIGN_CENTER,  
                        XSSFCellStyle.VERTICAL_BOTTOM);  
                // ## 水平居中,垂直居中 ##//  
                createCell(wb, row3, (short) 1,  
                        XSSFCellStyle.ALIGN_CENTER_SELECTION,  
                        XSSFCellStyle.VERTICAL_BOTTOM);  
                // ## 填充 ,垂直居中 ##//  
                createCell(wb, row3, (short) 2, XSSFCellStyle.ALIGN_FILL,  
                        XSSFCellStyle.VERTICAL_CENTER);  
                // ## 左对齐,垂直居中 ##//  
                createCell(wb, row3, (short) 3, XSSFCellStyle.ALIGN_GENERAL,  
                        XSSFCellStyle.VERTICAL_CENTER);  
                // ## 左对齐,顶端对齐 ##//  
                createCell(wb, row3, (short) 4, XSSFCellStyle.ALIGN_JUSTIFY,  
                        XSSFCellStyle.VERTICAL_JUSTIFY);  
                // ## 左对齐,顶端对齐 ##//  
                createCell(wb, row3, (short) 5, XSSFCellStyle.ALIGN_LEFT,  
                        XSSFCellStyle.VERTICAL_TOP);  
                // ## 右对齐,顶端对齐 ##//  
                createCell(wb, row3, (short) 6, XSSFCellStyle.ALIGN_RIGHT,  
                        XSSFCellStyle.VERTICAL_TOP);  
      
      
                /** 第四行 --- CELL边框 **/  
                Row row4 = sheet.createRow(3);  
                cell = row4.createCell(1);  
                cell.setCellValue(4);  
                cellStyle = wb.createCellStyle();  
                // ## 设置底部边框为THIN ##//  
                cellStyle.setBorderBottom(CellStyle.BORDER_THIN);  
                // ## 设置底部边框颜色为黑色 ##//  
                cellStyle.setBottomBorderColor(IndexedColors.BLACK.getIndex());  
                // ## 设置左边边框为THIN ##//  
                cellStyle.setBorderLeft(CellStyle.BORDER_THIN);  
                // ## 设置左边边框颜色为红色 ##//  
                cellStyle.setLeftBorderColor(IndexedColors.RED.getIndex());  
                // ## 设置右边边框为THIN ##//  
                cellStyle.setBorderRight(CellStyle.BORDER_THIN);  
                // ## 设置右边边框颜色为蓝色 ##//  
                cellStyle.setRightBorderColor(IndexedColors.BLUE.getIndex());  
                // ## 设置顶部边框为MEDIUM DASHED ##//  
                cellStyle.setBorderTop(CellStyle.BORDER_MEDIUM_DASHED);  
                // ## 设置顶部边框颜色为黑色 ##//  
                cellStyle.setTopBorderColor(IndexedColors.BLACK.getIndex());  
                cell.setCellStyle(cellStyle);  
      
      
                /** 第五行 --- 填充与颜色 **/  
                Row row5 = sheet.createRow((short) 4);  
                // ## Aqua背景 ##//  
                cellStyle = wb.createCellStyle();  
                cellStyle.setFillBackgroundColor(IndexedColors.AQUA.getIndex());  
                // ## 设置填充模式为BIG SPOTS ##//  
                cellStyle.setFillPattern(CellStyle.BIG_SPOTS);  
                cell = row5.createCell((short) 1);  
                cell.setCellValue("Aqua背景");  
                cell.setCellStyle(cellStyle);  
      
      
                // ## 橙色前景色(相对 于CELL背景) ##//  
                cellStyle = wb.createCellStyle();  
                cellStyle.setFillForegroundColor(IndexedColors.ORANGE.getIndex());  
                // ## 设置填充模式为SOLID FOREGROUND ##//  
                cellStyle.setFillPattern(CellStyle.SOLID_FOREGROUND);  
                cell = row5.createCell((short) 2);  
                cell.setCellValue("橙色前景色");  
                cell.setCellStyle(cellStyle);  
      
      
                /** 第六行 --- 合并单元格 **/  
                Row row6 = sheet.createRow((short) 5);  
                cell = row6.createCell((short) 4);  
                cell.setCellValue("合并单元格测试");  
                // ## Wrong:EXCEL 2007中打开workbook.xls文件看不到"合并单元格测试",但单元格已经合并了 ##//  
                /* 
                 * sheet.addMergedRegion(new CellRangeAddress( 3, //first row 
                 * (0-based) 5, //last row (0-based) 4, //first column (0-based) 6 
                 * //last column (0-based) )); 
                 */  
                // ## 正确合并单元格 注意:与上不同的是first row=last row ##//  
                sheet.addMergedRegion(new CellRangeAddress(5, // first row (0-based)  
                        5, // last row (0-based)  
                        4, // first column (0-based)  
                        6// last column (0-based)  
                        ));  
      
      
                /** 第七行 --- 字体 **/  
                Row row7 = sheet.createRow(6);  
                // ## 创建字体 ##//  
                // 注意:POI限制一个Workbook创建的Font对象最多为32767,所以不要为每个CELL创建一个字体,建议重用字体  
                font = wb.createFont();  
                // ## 设置字体大小为24 ##//  
                font.setFontHeightInPoints((short) 24);  
                // ## 设置字体样式为华文隶书 ##//  
                font.setFontName("华文隶书");  
                // ## 斜体 ##//  
                font.setItalic(true);  
                // ## 添加删除线 ##//  
                font.setStrikeout(true);  
                // ## 将字体添加到样式中 ##//  
                cellStyle = wb.createCellStyle();  
                cellStyle.setFont(font);  
                cell = row7.createCell(1);  
                cell.setCellValue("字体测试");  
                cell.setCellStyle(cellStyle);  
      
      
                /** 第八行 --- 自定义颜色 **/  
                Row row8 = sheet.createRow(7);  
                cell = row8.createCell(0);  
                cell.setCellValue("自定义颜色测试");  
                cellStyle = wb.createCellStyle();  
                // ## 设置填充前景色为LIME ##//  
                cellStyle.setFillForegroundColor(HSSFColor.LIME.index);  
                // ## 设置填充模式为SOLID FOREGROUND ##//  
                cellStyle.setFillPattern(CellStyle.SOLID_FOREGROUND);  
                font = wb.createFont();  
                // ## 设置字体颜色为红色 ##//  
                font.setColor(HSSFColor.RED.index);  
                cellStyle.setFont(font);  
                cell.setCellStyle(cellStyle);  
      
      
                /* 
                 * cell.setCellValue("自定义颜色测试Palette"); //creating a custom palette 
                 * for the workbook HSSFPalette palette = 
                 * ((HSSFWorkbook)wb).getCustomPalette(); //replacing the standard 
                 * red with freebsd.org red 
                 * palette.setColorAtIndex(HSSFColor.RED.index, (byte) 153, //RGB 
                 * red (0-255) (byte) 0, //RGB green (byte) 0 //RGB blue ); 
                 * //replacing lime with freebsd.org gold 
                 * palette.setColorAtIndex(HSSFColor.LIME.index, (byte) 255, (byte) 
                 * 204, (byte) 102); 
                 */  
      
      
                /** 第九行 --- 换行 **/  
                Row row9 = sheet.createRow(8);  
                cell = row9.createCell(2);  
                cell.setCellValue("使用 /n及Word-wrap创建一个新行");  
                cellStyle = wb.createCellStyle();  
                // ## 设置WrapText为true ##//  
                cellStyle.setWrapText(true);  
                cell.setCellStyle(cellStyle);  
                // ## 设置行的高度以适应新行 ---两行##//  
                row9.setHeightInPoints((2 * sheet.getDefaultRowHeightInPoints()));  
                // ## 调整列宽 ##//  
                sheet.autoSizeColumn(2);  
      
      
                /** 第十行 --- 数据格式 **/  
                DataFormat format = wb.createDataFormat();  
      
      
                Row row10 = sheet.createRow(9);  
                cell = row10.createCell(0);  
                cell.setCellValue(11111.25);  
                cellStyle = wb.createCellStyle();  
                // ## 一位小数 ##//  
                cellStyle.setDataFormat(format.getFormat("0.0"));  
                cell.setCellStyle(cellStyle);  
      
      
                cell = row10.createCell(1);  
                cell.setCellValue(11111.25);  
                cellStyle = wb.createCellStyle();  
                // ## 四位小数,千位逗号隔开 ##//  
                // #,###.0000效果一样  
                cellStyle.setDataFormat(format.getFormat("#,##0.0000"));  
                cell.setCellStyle(cellStyle);  
      
      
                // ## 将文件写到硬盘上 ##//  
                fileOut = new FileOutputStream(XLS_WORKBOOK_LOCATION);  
                wb.write(fileOut);  
                fileOut.close();  
            } catch (FileNotFoundException e) {  
                e.printStackTrace();  
            } catch (IOException e) {  
                e.printStackTrace();  
            }  
      
      
            /** 
             * EXCEL 2007及以后 
             */  
            /* 
             * try { wb = new XSSFWorkbook(); wb.createSheet("sheet1"); Cell cell = 
             * row.createCell( 0); cell.setCellValue("custom XSSF colors"); 
             * CellStyle style1 = wb.createCellStyle(); 
             * style1.setFillForegroundColor(new XSSFColor(new java.awt.Color(128, 
             * 0, 128))); style1.setFillPattern(CellStyle.SOLID_FOREGROUND); fileOut 
             * = new FileOutputStream("d:/workbook.xlsx"); wb.write(fileOut); 
             * fileOut.close(); } catch (FileNotFoundException e) { 
             * e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } 
             */  
      
      
        }  
      
      
        /** 
         * 创建相应格式的CELL 
         */  
        public void createCell(Workbook wb, Row row, short column, short halign,  
                short valign) {  
            Cell cell = row.createCell(column);  
            // ## 给CELL赋值 ##//  
            cell.setCellValue("对齐排列");  
            CellStyle cellStyle = wb.createCellStyle();  
            // ## 设置水平对齐方式 ##//  
            cellStyle.setAlignment(halign);  
            // ## 设置垂直对齐方式 ##//  
            cellStyle.setVerticalAlignment(valign);  
            // ## 添加CELL样式 ##//  
            cell.setCellStyle(cellStyle);  
        }  
      
      
        /** 
         * 测试POI EXCEL迭代和或CELL中的值 
         */  
        @Test  
        public void testExcelIteratorAndCellContents() {  
            try {  
                // ## 创建HSSFWorkbook实例 ##//  
                Workbook wb = new HSSFWorkbook(new FileInputStream(  
                        XLS_WORKBOOK_LOCATION));  
                // ## 获得第一个SHEET ##//  
                Sheet sheet = wb.getSheetAt(0); // or we could cast into  
                // HSSFSheet,that doesn't matter  
                /** 第一种迭代方法 **/  
                /* 
                 * //## 迭代ROW ##// for (Iterator<Row> rit = sheet.rowIterator(); 
                 * rit.hasNext(); ) { Row row = rit.next(); //## 迭代CELL ##// for 
                 * (Iterator<Cell> cit = row.cellIterator(); cit.hasNext(); ) { Cell 
                 * cell = cit.next(); System.out.println(cell); } } 
                 */  
                /** 第二种迭代方法 **/  
                for (Row row : sheet) {  
                    for (Cell cell : row) {  
                        // ## 获取CellReference对象 ##/  
                        CellReference cellRef = new CellReference(row.getRowNum(),  
                                cell.getColumnIndex());  
                        System.out.print(cellRef.formatAsString());  
                        System.out.print(" - ");  
                        // ## 根据CELL值类型进行相应处理 ##/  
                        switch (cell.getCellType()) {  
                        case Cell.CELL_TYPE_STRING:  
                            System.out.println(cell.getRichStringCellValue()  
                                    .getString());  
                            break;  
                        case Cell.CELL_TYPE_NUMERIC:  
                            // ## yyyy年mm月dd日 hh:mm:ss此种格式日期不能识别 ##//  
                            // ## mm/dd/yyyy h:mm,yyyy-MM-dd  
                            // hh:mm:ss可以识别,估计是POI对中文日期支持不怎么好的问题 ##//  
                            if (DateUtil.isCellDateFormatted(cell)) {  
                                System.out.println(cell.getDateCellValue());  
                            } else {  
                                System.out.println(cell.getNumericCellValue());  
                            }  
                            break;  
                        case Cell.CELL_TYPE_BOOLEAN:  
                            System.out.println(cell.getBooleanCellValue());  
                            break;  
                        case Cell.CELL_TYPE_FORMULA:  
                            System.out.println(cell.getCellFormula());  
                            break;  
                        case Cell.CELL_TYPE_ERROR:  
                            System.out.println(cell.getErrorCellValue());  
                            break;  
                        default:  
                            System.out.println();  
                        }  
                    }  
                }  
      
      
            } catch (FileNotFoundException e) {  
                e.printStackTrace();  
            } catch (IOException e) {  
                e.printStackTrace();  
            }  
        }  
      
      
        /** 
         * 修改文件测试 
         */  
        @Test  
        public void testReadingAndRewritingWorkbooks() {  
            InputStream inp = null;  
            try {  
                inp = new FileInputStream(XLS_WORKBOOK_LOCATION);  
                // inp = new FileInputStream("workbook.xlsx");  
                // ## 获得要修改的Workbook ##/  
                Workbook wb = WorkbookFactory.create(inp);  
                // ## 获取要修改的Sheet ##//  
                Sheet sheet = wb.getSheetAt(0);  
                // ## 获取要修改的Row ##//  
                Row row = sheet.getRow(1);  
                // ## 获取要修改的Cell,如果没有相应位置的Cell那么就创建一个 ##//  
                Cell cell = row.getCell(2);  
                if (cell == null)  
                    cell = row.createCell(2);  
                // ## 写入修改数据 ##//  
                cell.setCellType(Cell.CELL_TYPE_STRING);  
                cell.setCellValue("修改文件测试");  
      
      
                // ## 将文件写到硬盘上 ##//  
                FileOutputStream fileOut = new FileOutputStream(  
                        XLS_WORKBOOK_LOCATION);  
                wb.write(fileOut);  
                fileOut.close();  
            } catch (FileNotFoundException e) {  
                e.printStackTrace();  
            } catch (InvalidFormatException e) {  
                e.printStackTrace();  
            } catch (IOException e) {  
                e.printStackTrace();  
            }  
        }  
      
      
        /** 
         * 暂时没看到有什么区别 
         */  
        @Test  
        public void testFitSheetToOnePage() {  
            try {  
                Workbook wb = new HSSFWorkbook();  
                Sheet sheet = wb.createSheet("format sheet");  
                PrintSetup ps = sheet.getPrintSetup();  
      
      
                sheet.setAutobreaks(true);  
      
      
                ps.setFitHeight((short) 1);  
                ps.setFitWidth((short) 1);  
                // Create various cells and rows for spreadsheet.  
                FileOutputStream fileOut = new FileOutputStream(  
                        XLS_WORKBOOK_LOCATION);  
                wb.write(fileOut);  
                fileOut.close();  
            } catch (Exception e) {  
                e.printStackTrace();  
            }  
        }  
      
      
        /** 
         * 设置打印区域测试 
         */  
        @Test  
        public void testSetPrintArea() {  
            /** 
             * 注意:我测试的时候用的是EXCEL 2007打开的,效果不明显,只能控制列且列好像也是不正确的。 但是我用EXCEL 
             * 2007转换了一下,xls,xlsx的都正确了,目前还不知道是什么问题。 
             */  
            try {  
                Workbook wb = new HSSFWorkbook();  
                Sheet sheet = wb.createSheet("Print Area Sheet");  
                Row row = sheet.createRow(0);  
                row.createCell(0).setCellValue("第一个单元格");  
                row.createCell(1).setCellValue("第二个单元格");  
                row.createCell(2).setCellValue("第三个单元格");  
                row = sheet.createRow(1);  
                row.createCell(0).setCellValue("第四个单元格");  
                row.createCell(1).setCellValue("第五个单元格");  
                row = sheet.createRow(2);  
                row.createCell(0).setCellValue("第六个单元格");  
                row.createCell(1).setCellValue("第七个单元格");  
                // ## 设置打印区域 A1--C2 ##//  
                // wb.setPrintArea(0, "$A$1:$C$2");  
                // ## 或者使用以下方法设置 ##//  
                wb.setPrintArea(0, // Sheet页  
                        0, // 开始列  
                        2, // 结束列  
                        0, // 开始行  
                        1// 结束行  
                        );  
      
      
                FileOutputStream fileOut = new FileOutputStream(  
                        XLS_WORKBOOK_LOCATION);  
                wb.write(fileOut);  
                fileOut.close();  
            } catch (FileNotFoundException e) {  
                e.printStackTrace();  
            } catch (IOException e) {  
                e.printStackTrace();  
            }  
      
      
        }  
      
      
        /** 
         * 设置页脚测试 用“页面布局”可以看到效果 下列代码只适用xls 
         */  
        @Test  
        public void testSetPageNumbersOnFooter() {  
            try {  
                HSSFWorkbook wb = new HSSFWorkbook();  
                HSSFSheet sheet = wb.createSheet("Footer Test");  
                // ## 获得页脚 ##/  
                HSSFFooter footer = sheet.getFooter();  
                Row row;  
                // ## 将 当前页/总页数 写在右边 ##/  
                footer.setRight(HSSFFooter.page() + "/" + HSSFFooter.numPages());  
                for (int i = 0; i < 100; i++) {  
                    row = sheet.createRow(i);  
                    for (int j = 0; j < 20; j++) {  
                        row.createCell(j).setCellValue("A" + i + j);  
                    }  
                }  
                FileOutputStream fileOut = new FileOutputStream(  
                        XLS_WORKBOOK_LOCATION);  
                wb.write(fileOut);  
                fileOut.close();  
            } catch (FileNotFoundException e) {  
                e.printStackTrace();  
            } catch (IOException e) {  
                e.printStackTrace();  
            }  
      
      
        }  
      
      
        /** 
         * 测试一些POI提供的比较方便的函数 文档中有些以HSSF为前缀的类的方法以过时(e.g: HSSFSheet, HSSFCell etc.), 
         * 测试的时候我去掉了HSSF前缀,当然也就是现在POI推荐的接口(Sheet,Row,Cell etc.) 
         */  
        @Test  
        public void testConvenienceFunctions() {  
            try {  
                Workbook wb = new HSSFWorkbook();  
                Sheet sheet1 = wb.createSheet("Convenience Functions");  
                // ## 设置Sheet的显示比例 这里是3/4,也就是 75% ##//  
                sheet1.setZoom(3, 4);  
                // ## 合并单元格 ##//  
                Row row = sheet1.createRow((short) 1);  
                Row row2 = sheet1.createRow((short) 2);  
                Cell cell = row.createCell((short) 1);  
                cell.setCellValue("合并单元格测试");  
                // ## 创建合并区域 ##//  
                CellRangeAddress region = new CellRangeAddress(1, (short) 1, 4,  
                        (short) 4);  
                sheet1.addMergedRegion(region);  
      
      
                // ## 设置边框及边框颜色 ##//  
                final short borderMediumDashed = CellStyle.BORDER_MEDIUM_DASHED;  
                RegionUtil.setBorderBottom(borderMediumDashed, region, sheet1, wb);  
                RegionUtil.setBorderTop(borderMediumDashed, region, sheet1, wb);  
                RegionUtil.setBorderLeft(borderMediumDashed, region, sheet1, wb);  
                RegionUtil.setBorderRight(borderMediumDashed, region, sheet1, wb);  
                // ## 设置底部边框的颜色 ##//  
                RegionUtil.setBottomBorderColor(HSSFColor.AQUA.index, region,  
                        sheet1, wb);  
                // ## 设置顶部边框的颜色 ##//  
                RegionUtil.setTopBorderColor(HSSFColor.AQUA.index, region, sheet1,  
                        wb);  
                // ## 设置左边边框的颜色 ##//  
                RegionUtil.setLeftBorderColor(HSSFColor.AQUA.index, region, sheet1,  
                        wb);  
                // ## 设置右边边框的颜色 ##//  
                RegionUtil.setRightBorderColor(HSSFColor.AQUA.index, region,  
                        sheet1, wb);  
      
      
                // ## CellUtil的一些用法 ##/  
                CellStyle style = wb.createCellStyle();  
                style.setIndention((short) 10);  
                CellUtil.createCell(row, 8, "CellUtil测试", style);  
                Cell cell2 = CellUtil.createCell(row2, 8, "CellUtil测试");  
                // ## 设置对齐方式为居中对齐 ##//  
                CellUtil.setAlignment(cell2, wb, CellStyle.ALIGN_CENTER);  
      
      
                // ## 将Workbook写到硬盘上 ##//  
                FileOutputStream fileOut = new FileOutputStream(  
                        XLS_WORKBOOK_LOCATION);  
                wb.write(fileOut);  
                fileOut.close();  
            } catch (FileNotFoundException e) {  
                e.printStackTrace();  
            } catch (IOException e) {  
                e.printStackTrace();  
            }  
        }  
      
      
        /** 
         * 测试冻结窗格和拆分 
         */  
        @Test  
        public void testSplitAndFreezePanes() {  
            try {  
                Workbook wb = new HSSFWorkbook();  
                Sheet sheet1 = wb.createSheet("冻结首行Sheet");  
                Sheet sheet2 = wb.createSheet("冻结首列Sheet");  
                Sheet sheet3 = wb.createSheet("冻结两行两列 Sheet");  
                Sheet sheet4 = wb.createSheet("拆分Sheet");  
      
      
                /** 冻结窗格 **/  
                /* 
                 * createFreezePane( colSplit, rowSplit, topRow, leftmostColumn ) 
                 * colSplit 冻结线水平位置 rowSplit 冻结线垂直位置 topRow Top row visible in 
                 * bottom pane leftmostColumn Left column visible in right pane. 
                 */  
                // ## 冻结首行 ##//  
                sheet1.createFreezePane(0, 1, 0, 1);  
                // ## 冻结首列 ##//  
                sheet2.createFreezePane(1, 0, 1, 0);  
                // ## 冻结两行两列 ##//  
                sheet3.createFreezePane(2, 2);  
                // ## 拆分,左下的为面板为激活状态 ##//  
                sheet4.createSplitPane(2000, 2000, 0, 0, Sheet.PANE_LOWER_LEFT);  
      
      
                FileOutputStream fileOut = new FileOutputStream(  
                        XLS_WORKBOOK_LOCATION);  
                wb.write(fileOut);  
                fileOut.close();  
            } catch (FileNotFoundException e) {  
                e.printStackTrace();  
            } catch (IOException e) {  
                e.printStackTrace();  
            }  
        }  
      
      
        /** 
         * 测试简单图形 
         */  
        @Test  
        public void testDrawingShapes() {  
            try {  
                Workbook wb = new HSSFWorkbook();  
                Sheet sheet = wb.createSheet("Drawing Shapes");  
                // ## 得到一个HSSFPatriarch对象,有点像画笔但是注意区别 ##//  
                HSSFPatriarch patriarch = (HSSFPatriarch) sheet  
                        .createDrawingPatriarch();  
                /* 
                 * 构造器: HSSFClientAnchor(int dx1, int dy1, int dx2, int dy2, short 
                 * col1, int row1, short col2, int row2) 描述: 
                 * 创建HSSFClientAnchor类的实例,设置该anchor的顶-左和底-右坐标(相当于锚点,也就是图像出现的位置,大小等). 
                 * Creates a new client anchor and sets the top-left and 
                 * bottom-right coordinates of the anchor. 参数: dx1 第一个单元格的x坐标 dy1 
                 * 第一个单元格的y坐标 dx2 第二个单元格的x坐标 dy2 第二个单元格的y坐标 col1 第一个单元格所在列 row1 
                 * 第一个单元格所在行 col2 第二个单元格所在列 row2 第二个单元格所在行 
                 */  
                HSSFClientAnchor anchor = new HSSFClientAnchor(0, 0, 512, 255,  
                        (short) 1, 0, (short) 1, 0);  
                // ## 通过HSSFClientAnchor类的对象创建HSSFSimpleShape的实例 ##//  
                HSSFSimpleShape shape = patriarch.createSimpleShape(anchor);  
                // ## 画个椭圆 ##//  
                shape.setShapeType(HSSFSimpleShape.OBJECT_TYPE_OVAL);  
      
      
                // ## 这几个是没问题的 ##//  
                // shape.setLineStyleColor(10,10,10);  
                // shape.setFillColor(90,10,200);  
                // shape.setLineStyleColor(HSSFColor.BLUE.index); //设置不了,尚不知原因  
                // ## 设置线条颜色为红色 ##//  
                // shape.setLineStyleColor(Color.BLUE.getRGB()); //搞不清楚为什是反的BLUE:红色  
                // RED:蓝色,是不是开发POI的有点色盲,JUST KIDDING!  
                // ## 设置填充颜色为灰色 ##//  
                shape.setFillColor(Color.GRAY.getRGB()); // 这个又可以  
                // ## 设置线条宽度为3pt ##//  
                shape.setLineWidth(HSSFShape.LINEWIDTH_ONE_PT * 3);  
                // ## 设置线条的样式为点式 ##//  
                shape.setLineStyle(HSSFShape.LINESTYLE_DOTSYS);  
      
      
                // ## 创建文本框并填充文字 “创建文本框” ##//  
                HSSFTextbox textbox = patriarch.createTextbox(new HSSFClientAnchor(  
                        0, 0, 0, 0, (short) 1, 1, (short) 2, 2));  
                RichTextString text = new HSSFRichTextString("创建文本框");  
                // ## 创建字体 ##//  
                Font font = wb.createFont();  
                // ## 斜体 ##//  
                font.setItalic(true);  
                // ## 设置字体颜色为蓝色 ##//  
                // font.setColor((short)Color.BLUE.getBlue()); not work  
                font.setColor(HSSFColor.BLUE.index);  
                // ## 添加字体 ##//  
                text.applyFont(font);  
                textbox.setString(text);  
      
      
                // ## 将文件写到硬盘上 ##//  
                FileOutputStream fileOut = new FileOutputStream(  
                        XLS_WORKBOOK_LOCATION);  
                wb.write(fileOut);  
                fileOut.close();  
            } catch (FileNotFoundException e) {  
                e.printStackTrace();  
            } catch (IOException e) {  
                e.printStackTrace();  
            }  
        }  
      
      
        /** 
         * 添加图片到工作薄测试 已测试PNG,JPG,GIF 
         */  
        @Test  
        public void testImages() {  
            try {  
                // ## 创建一个新的工作薄 ##//  
                Workbook wb = new XSSFWorkbook(); // or new HSSFWorkbook();  
      
      
                // ## 添加图片到该工作薄 ##//  
                InputStream is = new FileInputStream(IMAGE_LOCATION);  
                byte[] bytes = IOUtils.toByteArray(is);  
                int pictureIdx = wb.addPicture(bytes, Workbook.PICTURE_TYPE_JPEG);  
                is.close();  
      
      
                CreationHelper helper = wb.getCreationHelper();  
      
      
                // ## 创建一个名为“添加图片”的Sheet ##//  
                Sheet sheet = wb.createSheet("添加图片");  
      
      
                // ## 创建一个DrawingPatriarch实例 ##//  
                Drawing drawing = sheet.createDrawingPatriarch();  
      
      
                // ## 设置图片的形状,位置等 ##//  
                ClientAnchor anchor = helper.createClientAnchor();  
                // set top-left corner of the picture,  
                // subsequent call of Picture#resize() will operate relative to it  
                anchor.setCol1(3);  
                anchor.setRow1(2);  
                Picture pict = drawing.createPicture(anchor, pictureIdx);  
                // ## 自动设置图片的大小 注意:只支持PNG,JPG,GIF(BMP未测试)##//  
                pict.resize();  
      
      
                // ## 保存Workbook ##//  
                String file = "picture.xls";  
                if (wb instanceof XSSFWorkbook)  
                    file += "x";  
                FileOutputStream fileOut = new FileOutputStream(XLS_OR_XLSX_DIR  
                        + file);  
                wb.write(fileOut);  
                fileOut.close();  
            } catch (FileNotFoundException e) {  
                e.printStackTrace();  
            } catch (IOException e) {  
                e.printStackTrace();  
            }  
      
      
        }  
    }  
    View Code
  • 相关阅读:
    C语言笔记
    js学习笔记
    Javascript学习笔记
    Java基础知识
    使用 StackExchange.Redis 封装属于自己的 RedisHelper
    StackExchange.Redis 使用资料
    .NET平台下Redis使用(三)【ServiceStack.Redis学习】
    .NET平台下Redis使用(二)【StackExchange.Redis学习】
    Redis 详解 (一) StackExchange.Redis Client
    .NET中使用Redis
  • 原文地址:https://www.cnblogs.com/yang1018/p/7884952.html
Copyright © 2011-2022 走看看