POI 操作(新接口)

POI 生成XLS实例
转载至:
http://www.4ucode.com/Study/Topic/697242

  1 ackage test;
  2 
  3 import java.io.FileOutputStream;  
  4 import java.io.IOException;  
  5 import java.util.Date;  
  6 import org.apache.poi.hssf.usermodel.HSSFCell;  
  7 import org.apache.poi.hssf.usermodel.HSSFCellStyle;  
  8 import org.apache.poi.hssf.usermodel.HSSFDataFormat;  
  9 import org.apache.poi.hssf.usermodel.HSSFFont;
 10 import org.apache.poi.hssf.usermodel.HSSFRow;  
 11 import org.apache.poi.hssf.usermodel.HSSFSheet;  
 12 import org.apache.poi.hssf.usermodel.HSSFWorkbook;  
 13 import org.apache.poi.hssf.util.HSSFColor;  
 14 import org.apache.poi.ss.util.CellRangeAddress;
 15  
 16 public class CreateCells {  
 17     /** 
 18      * 文档对象 HSSFWorkbook  ;表单对象 HSSFSheet ;行对象 HSSFRow ;列对象 HSSFCell 
 19      * excell的格子单元 HSSFFont excell字体 HSSFName 名称 HSSFDataFormat 日期格式 HSSFHeader 
 20      * sheet头 HSSFFooter sheet尾 HSSFCellStyle cell样式 
 21      */ 
 22     public static void main(String[] args) throws IOException {
 23      // 建立新HSSFWorkbook对象  
 24         HSSFWorkbook workbook = new HSSFWorkbook();  
 25         // 建立新的sheet对象  
 26         // Create a row and put some cells in it.Rows are 0 based.
 27         HSSFSheet sheet = workbook.createSheet("表单1");
 28         // 建立新行  
 29         // Create a cell and put a value in it.  
 30         HSSFRow row = sheet.createRow((short) 0);
 31         //修改当前行 默认行高  列宽
 32         //行高
 33         sheet.setDefaultRowHeightInPoints(10);
 34         //列款宽
 35         sheet.setDefaultColumnWidth(10);
 36         //设置特定单元格的宽度
 37         sheet.setColumnWidth(4, 20*256);
 38         sheet.setColumnWidth(5, 30*256);
 39         sheet.setColumnWidth(6, 30*256);
 40        
 41         // 整数类型的cell样式  
 42         //HSSFDataFormat.getBuiltinFormat("0.00")  字符串的内容是   Excel有的格式
 43         HSSFCellStyle numStyle = workbook.createCellStyle();  
 44         numStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat("0"));
 45         //创建1列
 46         HSSFCell cellNum = row.createCell(0);
 47         cellNum.setCellValue(1);
 48         cellNum.setCellStyle(numStyle);
 49        
 50        
 51         // 浮点类型的cell样式
 52         HSSFCellStyle doubleStyle = workbook.createCellStyle();  
 53         doubleStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat("0.00"));
 54        
 55         HSSFCell cellDouble = row.createCell(1);
 56         cellDouble.setCellValue(1.2);
 57         cellDouble.setCellStyle(doubleStyle);
 58        
 59        
 60         //字符串类型的cell样式
 61         HSSFCellStyle stringStyle = workbook.createCellStyle();  
 62         stringStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat("G/通用格式"));
 63        
 64         HSSFCell cellString= row.createCell(2);
 65         cellString.setCellValue("test");
 66         cellString.setCellStyle(stringStyle);
 67        
 68         //添加cell布尔类型的值 
 69         row.createCell(3).setCellValue(true);
 70        
 71        
 72         //日期类型的cell样式   yyyy-m-d  h:mm:ss AM/PM
 73         HSSFCellStyle dateStyle = workbook.createCellStyle(); 
 74         dateStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat("m/d/yy h:mm"));
 75         HSSFCell dCell = row.createCell(4);
 76         dCell.setCellValue(new Date());
 77         dCell.setCellStyle(dateStyle);
 78        
 79        
 80         //设置cell编码解决中文高位字节截断
 81         HSSFCell csCell = row.createCell(5);
 82         csCell.setCellType(HSSFCell.ENCODING_UTF_16);
 83         csCell.setCellValue("中文测试_Chinese Words Test");  
 84  
 85         // 设置  背景色     边框
 86         HSSFCellStyle style1 = workbook.createCellStyle();
 87         //前景色和后景色都要有  否则会出网格
 88         style1.setFillForegroundColor(new HSSFColor.YELLOW().getIndex());  
 89         style1.setFillBackgroundColor(new HSSFColor.YELLOW().getIndex());  
 90         //设置边框
 91         style1.setBorderBottom((short) 1);  
 92         style1.setBorderTop((short) 1);
 93         style1.setBorderLeft((short) 1);
 94         style1.setBorderRight((short) 1);  
 95        
 96         //问题:用poi将一个cell中的字体设置成了红色,结果用excell打开后,这个cell中只有前面一个或几个字为红色
 97         //HSSFFont  font  =  workbook.createFont();  font.setColor(HSSFFont.COLOR_RED); 
 98         //先從Cell中把HSSFRichTextString取出來
 99         //然后HSSFRichTextString對象.applyFont(font)
100         //最后再把HSSFRichTextString對象set回到cell中就行了。。。。。
101        
102         //设置字体样式=====================================
103         HSSFFont  font  =  workbook.createFont();
104         //字体位置  上 下 左 右
105         //font.setTypeOffset((short)0);
106         //字体宽度
107         font.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL);
108         //字体高度
109         font.setFontHeightInPoints((short)8);
110         //字体颜色
111         font.setColor(HSSFFont.COLOR_RED); 
112         //=================================================
113         style1.setFont(font);
114        
115         /** 
116          * 注意这句代码, style1.setFillPattern, 如果你在你的程序中不设置fill pattern,那么 
117          * 你上面设置的前景色和背景色就显示不出来.网络上很多文章都没有设置fillpattern
118          * 如果不改变样式  不需要添加(如:居中)
119          */ 
120         style1.setFillPattern(HSSFCellStyle.SPARSE_DOTS);
121        
122        
123         HSSFCell cellCH = row.createCell(6);
124         cellCH.setCellValue("中文测试_Chinese Words Testsss");  
125         cellCH.setCellStyle(style1);
126        
127        
128         //货币样式
129         HSSFCellStyle moneyStyle = workbook.createCellStyle();  
130         moneyStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat("#,##0"));
131         HSSFCell cell12 = row.createCell(7);
132         cell12.setCellValue((double) 10000000);
133         cell12.setCellStyle(moneyStyle);
134  
135         // 错误显示
136         row.createCell(8).setCellType(HSSFCell.CELL_TYPE_ERROR);
137         //合并单元格
138         int startRowNo=0;
139         int endRowNo=0;
140         int startCellNo=9;
141         int endCellNo=10;
142         sheet.addMergedRegion(new CellRangeAddress(startRowNo, endRowNo,startCellNo, endCellNo));
143   HSSFCell cell = row.createCell(9);
144   cell.setCellValue("合并");
145   
146   //即垂直居中对齐且水平居中对齐    居中后背景颜色变化了
147   HSSFCellStyle style = workbook.createCellStyle();  
148   style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);//垂直  
149   style.setAlignment(HSSFCellStyle.ALIGN_CENTER);//水平  
150   //如果不改变样式  不需要添加
151   //style.setFillPattern(HSSFCellStyle.SPARSE_DOTS);
152   cell.setCellStyle(style);
153   
154         FileOutputStream fileOut = new FileOutputStream("e:/workbook.xls");  
155         workbook.write(fileOut);  
156         fileOut.close();  
157     }  
158 }  

原文地址:https://www.cnblogs.com/liuyq/p/3898605.html