`
ldsjdy
  • 浏览: 148154 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

POI 生成XLS实例

 
阅读更多
转载至:
http://www.4ucode.com/Study/Topic/697242

ackage test;

import java.io.FileOutputStream;  
import java.io.IOException;  
import java.util.Date;  
import org.apache.poi.hssf.usermodel.HSSFCell;  
import org.apache.poi.hssf.usermodel.HSSFCellStyle;  
import org.apache.poi.hssf.usermodel.HSSFDataFormat;  
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.hssf.util.HSSFColor;  
import org.apache.poi.ss.util.CellRangeAddress;
 
public class CreateCells {  
    /** 
     * 文档对象 HSSFWorkbook  ;表单对象 HSSFSheet ;行对象 HSSFRow ;列对象 HSSFCell 
     * excell的格子单元 HSSFFont excell字体 HSSFName 名称 HSSFDataFormat 日期格式 HSSFHeader 
     * sheet头 HSSFFooter sheet尾 HSSFCellStyle cell样式 
     */ 
    public static void main(String[] args) throws IOException {
     // 建立新HSSFWorkbook对象  
        HSSFWorkbook workbook = new HSSFWorkbook();  
        // 建立新的sheet对象  
        // Create a row and put some cells in it.Rows are 0 based.
        HSSFSheet sheet = workbook.createSheet("表单1");
        // 建立新行  
        // Create a cell and put a value in it.  
        HSSFRow row = sheet.createRow((short) 0);
        //修改当前行 默认行高  列宽
        //行高
        sheet.setDefaultRowHeightInPoints(10);
        //列款宽
        sheet.setDefaultColumnWidth(10);
        //设置特定单元格的宽度
        sheet.setColumnWidth(4, 20*256);
        sheet.setColumnWidth(5, 30*256);
        sheet.setColumnWidth(6, 30*256);
       
        // 整数类型的cell样式  
        //HSSFDataFormat.getBuiltinFormat("0.00")  字符串的内容是   Excel有的格式
        HSSFCellStyle numStyle = workbook.createCellStyle();  
        numStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat("0"));
        //创建1列
        HSSFCell cellNum = row.createCell(0);
        cellNum.setCellValue(1);
        cellNum.setCellStyle(numStyle);
       
       
        // 浮点类型的cell样式
        HSSFCellStyle doubleStyle = workbook.createCellStyle();  
        doubleStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat("0.00"));
       
        HSSFCell cellDouble = row.createCell(1);
        cellDouble.setCellValue(1.2);
        cellDouble.setCellStyle(doubleStyle);
       
       
        //字符串类型的cell样式
        HSSFCellStyle stringStyle = workbook.createCellStyle();  
        stringStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat("G/通用格式"));
       
        HSSFCell cellString= row.createCell(2);
        cellString.setCellValue("test");
        cellString.setCellStyle(stringStyle);
       
        //添加cell布尔类型的值 
        row.createCell(3).setCellValue(true);
       
       
        //日期类型的cell样式   yyyy-m-d  h:mm:ss AM/PM
        HSSFCellStyle dateStyle = workbook.createCellStyle(); 
        dateStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat("m/d/yy h:mm"));
        HSSFCell dCell = row.createCell(4);
        dCell.setCellValue(new Date());
        dCell.setCellStyle(dateStyle);
       
       
        //设置cell编码解决中文高位字节截断
        HSSFCell csCell = row.createCell(5);
        csCell.setCellType(HSSFCell.ENCODING_UTF_16);
        csCell.setCellValue("中文测试_Chinese Words Test");  
 
        // 设置  背景色     边框
        HSSFCellStyle style1 = workbook.createCellStyle();
        //前景色和后景色都要有  否则会出网格
        style1.setFillForegroundColor(new HSSFColor.YELLOW().getIndex());  
        style1.setFillBackgroundColor(new HSSFColor.YELLOW().getIndex());  
        //设置边框
        style1.setBorderBottom((short) 1);  
        style1.setBorderTop((short) 1);
        style1.setBorderLeft((short) 1);
        style1.setBorderRight((short) 1);  
       
        //问题:用poi将一个cell中的字体设置成了红色,结果用excell打开后,这个cell中只有前面一个或几个字为红色
        //HSSFFont  font  =  workbook.createFont();  font.setColor(HSSFFont.COLOR_RED); 
        //先從Cell中把HSSFRichTextString取出來
        //然后HSSFRichTextString對象.applyFont(font)
        //最后再把HSSFRichTextString對象set回到cell中就行了。。。。。
       
        //设置字体样式=====================================
        HSSFFont  font  =  workbook.createFont();
        //字体位置  上 下 左 右
        //font.setTypeOffset((short)0);
        //字体宽度
        font.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL);
        //字体高度
        font.setFontHeightInPoints((short)8);
        //字体颜色
        font.setColor(HSSFFont.COLOR_RED); 
        //=================================================
        style1.setFont(font);
       
        /** 
         * 注意这句代码, style1.setFillPattern, 如果你在你的程序中不设置fill pattern,那么 
         * 你上面设置的前景色和背景色就显示不出来.网络上很多文章都没有设置fillpattern
         * 如果不改变样式  不需要添加(如:居中)
         */ 
        style1.setFillPattern(HSSFCellStyle.SPARSE_DOTS);
       
       
        HSSFCell cellCH = row.createCell(6);
        cellCH.setCellValue("中文测试_Chinese Words Testsss");  
        cellCH.setCellStyle(style1);
       
       
        //货币样式
        HSSFCellStyle moneyStyle = workbook.createCellStyle();  
        moneyStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat("#,##0"));
        HSSFCell cell12 = row.createCell(7);
        cell12.setCellValue((double) 10000000);
        cell12.setCellStyle(moneyStyle);
 
        // 错误显示
        row.createCell(8).setCellType(HSSFCell.CELL_TYPE_ERROR);
        //合并单元格
        int startRowNo=0;
        int endRowNo=0;
        int startCellNo=9;
        int endCellNo=10;
        sheet.addMergedRegion(new CellRangeAddress(startRowNo, endRowNo,startCellNo, endCellNo));
  HSSFCell cell = row.createCell(9);
  cell.setCellValue("合并");
  
  //即垂直居中对齐且水平居中对齐    居中后背景颜色变化了
  HSSFCellStyle style = workbook.createCellStyle();  
  style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);//垂直  
  style.setAlignment(HSSFCellStyle.ALIGN_CENTER);//水平  
  //如果不改变样式  不需要添加
  //style.setFillPattern(HSSFCellStyle.SPARSE_DOTS);
  cell.setCellStyle(style);
  
        FileOutputStream fileOut = new FileOutputStream("e:/workbook.xls");  
        workbook.write(fileOut);  
        fileOut.close();  
    }  
}  
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics