




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、HSSFCell 设置样式1、遍历workbook / load源文件 POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream(filePath); HSSFWorkbook wb = new HSSFWorkbook(fs); for (int i = 0; i < wb.getNumberOfSheets(); i+) HSSFSheet sheet = wb.getSheetAt(i);
2、0; for (int j = sheet.getFirstRowNum(); j < sheet.getLastRowNum(); j+) HSSFRow row = sheet.getRow(j); if (row != null) / 。操作 &
3、#160; / 目标文件 FileOutputStream fos = new FileOutputStream(objectPath); / 写文件 wb.write(fos); fos.close(); 2、得到列和单元格 HSSFRow row = sheet.getRow(i); HSSFCell cell = row.getCell(short) j); 3
4、、设置sheet名称和单元格内容为中文 wb.setSheetName(n, "中文",HSSFCell.ENCODING_UTF_16); cell.setEncoding(short) 1); cell.setCellValue("中文"); 4、单元格内容未公式或数值,可以这样读写 cell.setCellType(HSSFCell.CELL_TY
5、PE_NUMERIC); cell.getNumericCellValue(); 5、设置列宽、行高 sheet.setColumnWidth(short)column,(short)width); row.setHeight(short)height); 6、添加区域,合并单元格 Region region = new Region(short)rowFrom,(short)columnFrom,(short)rowTo,(short)columnT
6、o); sheet.addMergedRegion(region); /得到所有区域 sheet.getNumMergedRegions() ; 7、常用方法 根据单元格不同属性返回字符串数值 public String getCellStringValue(HSSFCell cell) String cellValue = ""
7、160; switch (cell.getCellType() case HSSFCell.CELL_TYPE_STRING: cellValue = cell.getStringCellValue(); if (cellValue.trim().equals("") | cellValue.trim().length() <= 0)
8、; cellValue = " " break; case HSSFCell.CELL_TYPE_NUMERIC: cellValue = String.valueOf(cell.getNumericCellValue(); break; case HSSFCell.CELL_TYPE_FOR
9、MULA: cell.setCellType(HSSFCell.CELL_TYPE_NUMERIC); cellValue = String.valueOf(cell.getNumericCellValue(); break; case HSSFCell.CELL_TYPE_BLANK: cellValue =
10、" " break; case HSSFCell.CELL_TYPE_BOOLEAN: break; case HSSFCell.CELL_TYPE_ERROR: break; default: break;
11、; return cellValue; 8、常用单元格边框格式 虚线HSSFCellStyle.BORDER_DOTTED 实线HSSFCellStyle.BORDER_THIN public static HSSFCellStyle getCellStyle(short type) HSSFWorkbook wb = new HSSFWorkbook(); HSSFCellStyle style = wb.cre
12、ateCellStyle(); style.setBorderBottom(type);/ 下边框 style.setBorderLeft(type);/ 左边框 style.setBorderRight(type);/ 右边框 style.setBorderTop(type);/ 上边框 return style; 设置字体和内容位置 HSSFFont f
13、 = wb.createFont(); f.setFontHeightInPoints(short) 11);/ 字号 f.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL);/ 加粗 style.setFont(f); style.setAlignment(HSSFCellStyle.ALIGN_CENTER);/ 左右居中 style.setVerticalAlignm
14、ent(HSSFCellStyle.VERTICAL_CENTER);/ 上下居中 style.setRotation(short rotation);/ 单元格内容的旋转的角度 HSSFDataFormat df = wb.createDataFormat(); style1.setDataFormat(df.getFormat("0.00%");/ 设置单元格数据格式 cell.setCellFormula(string);/ 给单元格设公
15、式 style.setRotation(short rotation);/ 单元格内容的旋转的角度 cell.setCellStyle(style); 10、插入图片 / 先把读进来的图片放到一个ByteArrayOutputStream中,以便产生ByteArray ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream(); BufferedImage bufferImg = ImageIO
16、.read(new File("ok.jpg"); ImageIO.write(bufferImg, "jpg", byteArrayOut); / 读进一个excel模版 FileInputStream fos = new FileInputStream(filePathName + "/stencil.xlt"); fs = new POIFSFileSystem(fos); / 创建一个工作薄
17、60; HSSFWorkbook wb = new HSSFWorkbook(fs); HSSFSheet sheet = wb.getSheetAt(0); HSSFPatriarch patriarch = sheet.createDrawingPatriarch(); HSSFClientAnchor anchor = new HSSFClientAnchor(0, 0, 1023, 255, (short) 0, 0, (short) 10, 10); patriarch.createPic
18、ture(anchor, wb.addPicture(byteArrayOut.toByteArray(), HSSFWorkbook.PICTURE_TYPE_JPEG); java操作excel(HSSF样式一) import java.io.IOException;import java.io.PrintWriter;import java.util.ArrayList;import java.util.HashMap;import java.util.Iterator;import java.util.List;import javax.servlet.ServletExce
19、ption;import javax.servlet.ServletOutputStream;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.apache.poi.hssf.usermodel.HSSFCell;import org.apache.poi.hssf.usermodel.HSSFCellStyle;import org.apache.poi.hssf.
20、util.HSSFColor;import org.apache.poi.hssf.util.Region;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;HSSFCellStyle类代表一种单元格样式。可以通过这些类来设置单元格的边框样式、背景颜色、字体、水平和垂直方式
21、的对齐。如: HSSFWorkbook workbook = new HSSFWorkbook(); /建立一个工作薄 HSSFCellStyle titleStyle=workbook.createCellStyle(); /在工作薄的基础上建立一个样式
22、0; titleStyle.setBorderBottom(HSSFCellStyle.BORDER_DOUBLE); /设置边框样式 titleStyle.setBorderLeft(short)1); /左边框 titleStyle.s
23、etBorderRight(short)1); /右边框 titleStyle.setBorderTop(HSSFCellStyle.BORDER_DOUBLE); /顶边框 titleStyle.setFillForegroundColor(HSSFColor.LIGHT_ORANGE.index);
24、0; /填充的背景颜色 titleStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND); /填充图案假设什么定义了一个样式,想在填充第一个单元格的时候填充红,第二格单元格填充蓝色。如果: HSSFCellStyle cellStyle = workbook.createC
25、ellStyle(); /创建一个样式 cellStyle.setFillForegroundColor(HSSFColor.RED.index); /设置颜色为红色 cellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
26、60; HSSFCell cell1 = row.createCell(short)1); /给单元格cell1填充红色 cell1.setCellStyle(cellStyle);若: cellStyle.setFillForegroundColor(HSSFColor.BLUE.index);&
27、#160; /设置颜色为蓝色 HSSFCell cell2 = row.createCell(short)2); /给单元格cell2填充蓝色 cell2.setCellStyle(cellStyle);这个时候会出现的现象是单元格cel
28、l1和cell2都变成了蓝色。遇到这种情况,要预先定义两种不同的单元格样式。当一个EXCEL文件同时需要很多大同小异的单元格样式时,这样一一定义很麻烦。POI HSSF提供了一个HSSFCellUtil类(在org.apache.poi. hssf.usermodel.contrib包),里面有几个方法可以绕过HSSFCellStyle直接设定单元格的样式,但这几个方法会抛出NestableException异
29、60; 常,要处理这个异常,需要引用Apache的几个Common包:commons-beanutils.jarcommons-beanutils-bean-collections.jarcommons-beanutils-core.jarcommons-lang.jarcommons-logging-api.jar以下是其他各种情况的处理:1、中文处理:要在通过POI生成的EXCEL中正常显示中文,需要为单元格设置编码: cell.set
30、Encoding(HSSFCell.ENCODING_UTF_16); cell.setCellValue("部门");2、合并单元格:HSSFSheet.addMergedRegion(new Region()方法可以合并单元格,Region()中的一个构造函数含有四个参数,分别代表起始行、起始列、结束 行、结束列:
31、60; sheet.addMergedRegion(new Region(initRow, (short)(initCol-2), initRow + lists.size() - 1, (short)(initCol-2);3、公式的处理:HSSFCell.setCellFormula()方法用来在EXCEL单元格中写入公式。 cell = row.createCell(short)(dataFlag);
32、; cell.setCellType(HSSFCell.CELL_TYPE_FORMULA); cell.setCellFormula("SUM(" + getColLetter(initCol) + (listFlag+1) +":" + getColLetter(dataFlag-1) + (listFlag+1) +
33、0; ")"); cell.setCellStyle(nameStyle);4、链接的处理:在POI中往单元格中写链接,是用HYPERLINK函数搞定的。HYPERLINK函数包含两个参数,第一个参数是指向的URL地址,第二个参数是显示字串。 &
34、#160; cell = row.createCell(short)(dataFlag); cell.setCellType(HSSFCell.CELL_TYPE_FORMULA); cell.setCellFormula("HYPERLINK(/" cell.setCellStyle(linkStyle); 为了使链接效果更好,我们可以给链接所在单元格定义一种样式,使链接显示为有下划线的蓝色字串:
35、; HSSFCellStyle linkStyle = workbook.createCellStyle(); linkStyle.setBorderBottom(short)1); linkStyle.setBorderLeft(short)1); linkStyle.setBorderRight(short)1); linkStyle.setBorderTop(short)1); linkStyle.setFil
36、lForegroundColor(HSSFColor.SKY_BLUE.index); linkStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND); HSSFFont font = workbook.createFont(); font.setFontName(HSSFFont.FONT_ARIAL); font.setUnderline(byte)1); fon
37、t.setColor(HSSFColor.BLUE.index); linkStyle.setFont(font); 另:package *.*; import java.util.ArrayList; import java.util.Iterator; import org.apache.poi.hssf.usermodel.HSSFCell;
38、0; import org.apache.poi.hssf.usermodel.HSSFCellStyle; 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.userm
39、odel.HSSFWorkbook; public class FontCellStyle private static HSSFFont fontStyle = null; private static HSSFCellStyle cellStyle = null; /*设置字体格式*/ public static HSSFFont getHdrFont(HSSFWorkb
40、ook wb) fontStyle = wb.createFont(); fontStyle.setFontName("宋体"); fontStyle.setFontHeightInPoints(short)20); fontStyle.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); return fontStyle;
41、60; public static HSSFFont getFtrFont(HSSFWorkbook wb) fontStyle = wb.createFont(); fontStyle.setFontName("宋体"); fontStyle.setFontHeightInPoints(short)12); fontStyle.setBoldweight(HSSFFont.BOLDWEIGHT_NORMA
42、L); return fontStyle; public static HSSFFont getContentFont(HSSFWorkbook wb) fontStyle = wb.createFont(); fontStyle.setFontName("宋体"); fontStyle.setFontHeightInPoints(short)12);
43、60; fontStyle.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL); return fontStyle; public static HSSFFont getMergeConflictFont(HSSFWorkbook wb) fontStyle = wb.createFont(); fontStyle.setFontName("Ari
44、al"); fontStyle.setFontHeightInPoints(short)12); fontStyle.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL); fontStyle.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); return fontStyle; /*设置Excel单元格格式,
45、引用到字体格式*/ public static HSSFCellStyle getAnyCellStyle(HSSFWorkbook wb,HSSFFont font,short align,short valign,short indent,boolean wrapText) cellStyle =wb.createCellStyle(); if(font != null) cellStyle.setFont(font); if(align > 0) c
46、ellStyle.setAlignment(align); if(valign > 0) cellStyle.setVerticalAlignment(valign); if(indent > 0) cellStyle.setIndention(indent); cellStyle.setWrapText(wrapText); return cellStyle;
47、160; /*设置Excel单元格行高、列宽*/ public static void setDefaultHighWidth(HSSFSheet sheet) sheet.setDefaultRowHeightInPoints(10); sheet.setDefaultColumnWidth(short) 20); public static void setDefaultCellHighWidthInRang
48、e(HSSFSheet sheet,short eachCellWidth,int high) /假定第一行和第一行所需的单元个已经建立好了,也就是说,在这之前已经调用了DesignXlsHeaderFooter.setXlsHeader sheet.setDefaultRowHeightInPoints(high);/设置默认高 /*设置各列单元格宽度*/ for(int i = 0;i < eachCellWidth.length;i+)
49、 /System.out.print(""+i+"/t"); sheet.setColumnWidth(short) i,(short) (eachCellWidthi)*256); /System.out.println(); /* Iterator arrayItr = eachCellWidth.iterator(); short width; short pos = 0; whi
50、le(arrayItr.hasNext() width = Short.parseShort(String)arrayItr.next(); sheet.setColumnWidth(pos,width); pos+; */ /end_setDefaultCellHighWidthInRange /*调用方式*/ /*设置整体excel单元格格式*/
51、; FileOutputStream fos = null; try fos = new FileOutputStream(rptRealPathAndName); catch (FileNotFoundException e) / TODO Auto-generated catch block /System.out.println("创建文件失败。");
52、 ("In WriteRptByType.writeRptTypeFive(),create file failed!"); log.error(e.getMessage(); /e.printStackTrace(); return -1; HSSFWorkbook workBook = new HSSFWorkbook(); HSSFShe
53、et sheet = workBook.createSheet(); workBook.setSheetName(0,"移动",HSSFWorkbook.ENCODING_UTF_16); HSSFCellStyle cellStyleHdr = FontCellStyle.getAnyCellStyle(workBook,FontCellStyle.getHdrFont(workBook),HSSFCellStyle.ALIGN_CENTER, HSSFCellStyle.VERTICAL_CENTE
54、R, (short)-1, true); HSSFRow curRow = sheet.createRow(0); HSSFCell curCell= curRow.createCell(short)0); curCell.setEncoding(HSSFCell.ENCODING_UTF_16); curCell.setCellStyle(cellStyleHdr ); curCell.s
55、etCellValue("可以写入汉字,无乱码"); /*.写入文件.*/ try workBook.write(fos); fos.close(); catch (IOException e) / TODO Auto-generated catch block /System.ou
56、t.println("写错误。"); succFlag = -1; log.error("报表写错误:"+e.getMessage(); /e.printStackTrace(); java操作excel(HSSF样式一) import java.io.IOException;import java.io.PrintWriter;import java.util.ArrayList;import java.uti
57、l.HashMap;import java.util.Iterator;import java.util.List;import javax.servlet.ServletException;import javax.servlet.ServletOutputStream;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.apache.poi.hssf.usermod
58、el.HSSFCell;import org.apache.poi.hssf.usermodel.HSSFCellStyle;import org.apache.poi.hssf.util.HSSFColor;import org.apache.poi.hssf.util.Region;import org.apache.poi.hssf.usermodel.HSSFFont;import org.apache.poi.hssf.usermodel.HSSFRow;import org.apache.poi.hssf.usermodel.HSSFSheet;import org.apache.
59、poi.hssf.usermodel.HSSFWorkbook;HSSFCellStyle类代表一种单元格样式。可以通过这些类来设置单元格的边框样式、背景颜色、字体、水平和垂直方式的对齐。如: HSSFWorkbook workbook = new HSSFWorkbook(); /建立一个工作薄 HSSFCellStyle titleStyle
60、=workbook.createCellStyle(); /在工作薄的基础上建立一个样式 titleStyle.setBorderBottom(HSSFCellStyle.BORDER_DOUBLE); /设置边框样式 titleStyle.setBorderLeft(short)1);
61、 /左边框 titleStyle.setBorderRight(short)1); /右边框 titleStyle.setBorderTop(HSSFCellStyle.BORDER_DOUBLE); /顶边框
62、60; titleStyle.setFillForegroundColor(HSSFColor.LIGHT_ORANGE.index); /填充的背景颜色 titleStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND); /填充图案假设什么定义了一个样式,想在填充第一个单元格的时候填充红,第二格单元格填充蓝色。如果: &
63、#160; HSSFCellStyle cellStyle = workbook.createCellStyle(); /创建一个样式 cellStyle.setFillForegroundColor(HSSFColor.RED.index); /设置颜色为红色 &
64、#160; cellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND); HSSFCell cell1 = row.createCell(short)1); /给单元格cell1填充红色 cell1.setCellStyle(cellStyle);若:
65、60; cellStyle.setFillForegroundColor(HSSFColor.BLUE.index); /设置颜色为蓝色 HSSFCell cell2 = row.createCell(short)2); /给单元格cell2填充蓝色
66、 cell2.setCellStyle(cellStyle);这个时候会出现的现象是单元格cell1和cell2都变成了蓝色。遇到这种情况,要预先定义两种不同的单元格样式。当一个EXCEL文件同时需要很多大同小异的单元格样式时,这样一一定义很麻烦。POI HSSF提供了一个HSSFCellUtil类(在org.apache.poi. hssf.usermodel.contrib包),里面有几个
67、方法可以绕过HSSFCellStyle直接设定单元格的样式,但这几个方法会抛出NestableException异 常,要处理这个异常,需要引用Apache的几个Common包:commons-beanutils.jarcommons-beanutils-bean-collections.jarcommons-beanutils-core.jarcommons-lang.jarcommons-logging-api.jar以下是其他各种情况的处理:1、中文处理:要在通过POI
68、生成的EXCEL中正常显示中文,需要为单元格设置编码: cell.setEncoding(HSSFCell.ENCODING_UTF_16); cell.setCellValue("部门");2、合并单元格:HSSFSheet.addMergedRegion(new Region()方法可以合并单元格,Region()中的一个构造函数含有四个参数,分别代表起始
69、行、起始列、结束行、结束列:sheet.addMergedRegion(new Region(initRow, (short)(initCol-2), initRow + lists.size() - 1, (short)(initCol-2);3、公式的处理:HSSFCell.setCellFormula()方法用来在EXCEL单元格中写入公式。cell = row.createCell(short)(dataFlag);cell.setCellType(HSSFCell.CELL_TYPE_FORMULA);cell.setCellFormula("SUM(" + ge
70、tColLetter(initCol) + (listFlag+1) +":" + getColLetter(dataFlag-1) + (listFlag+1) +")");cell.setCellStyle(nameStyle);4、链接的处理:在POI中往单元格中写链接,是用HYPERLINK函数搞定的。HYPERLINK函数包含两个参数,第一个参数是指向的URL地址,第二个参数是显示字串。 cell = row.createCell(short)(dataFlag);
71、0; cell.setCellType(HSSFCell.CELL_TYPE_FORMULA); cell.setCellFormula("HYPERLINK(" cell.setCellStyle(linkStyle); 为了使链接效果更好,我们可以给链接所在单元格定义一种样式,使链接显示为有下划线的蓝色字串:HSSFCellStyle linkStyle = workbook.createCellStyle();linkStyle.setBorderBottom(
72、short)1);linkStyle.setBorderLeft(short)1);linkStyle.setBorderRight(short)1);linkStyle.setBorderTop(short)1);linkStyle.setFillForegroundColor(HSSFColor.SKY_BLUE.index);linkStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);HSSFFont font = workbook.createFont(); font.setFontName(HS
73、SFFont.FONT_ARIAL); font.setUnderline(byte)1); font.setColor(HSSFColor.BLUE.index); linkStyle.setFont(font); 另:package *.*; import java.util.ArrayList; import java.util.Iterator;
74、160; import org.apache.poi.hssf.usermodel.HSSFCell; import org.apache.poi.hssf.usermodel.HSSFCellStyle; import org.apache.poi.hssf.usermodel.HSSFFont; import org.apache.poi.hssf.usermodel.HSSFRow; import org.a
75、pache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFWorkbook; public class FontCellStyle private static HSSFFont fontStyle = null; private static HSSFCellStyle cellStyle = null;
76、 /*设置字体格式*/ public static HSSFFont getHdrFont(HSSFWorkbook wb) fontStyle = wb.createFont(); fontStyle.setFontName("宋体"); fontStyle.setFontHeightInPoints(short)20); fontStyle.setBoldweigh
77、t(HSSFFont.BOLDWEIGHT_BOLD); return fontStyle; public static HSSFFont getFtrFont(HSSFWorkbook wb) fontStyle = wb.createFont(); fontStyle.setFontName("宋体"); fontStyle.setFontHeightI
78、nPoints(short)12); fontStyle.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL); return fontStyle; public static HSSFFont getContentFont(HSSFWorkbook wb) fontStyle = wb.createFont(); fontStyle.setFon
79、tName("宋体"); fontStyle.setFontHeightInPoints(short)12); fontStyle.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL); return fontStyle; public static HSSFFont getMergeConflictFont(HSSFWorkbook wb) fontStyle = wb.createFont(); fontStyle.setFontName("Arial&qu
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 快递安全生产培训
- 华北理工大学《建筑工程安全技术与管理》2023-2024学年第二学期期末试卷
- 福建对外经济贸易职业技术学院《科技论文写作及文献检索》2023-2024学年第二学期期末试卷
- 信息技术 第二册(五年制高职)课件 9.2.2 计算机视觉的定义
- 医院安全消防
- 手术室护理评估
- 以课件促高效课堂
- 2025房地产经纪人《房地产经纪业务操作》核心备考题库(含典型题、重点题)
- 呀诺达旅游景点
- 开学第一课安全知识
- 江苏建设工程质量检测和建筑材料试验收费标准苏价服
- 中国严重脓毒症脓毒性休克治疗指南2023年
- 采购部采购管理制度
- 院感知识培训新
- 《文学概论》课程教学大纲
- 2023年山东专升本计算机真题及答案
- WB/T 1019-2002菱镁制品用轻烧氧化镁
- LS/T 6118-2017粮油检验稻谷新鲜度测定与判别
- GB/T 1957-2006光滑极限量规技术条件
- 公路安全员b证考试试题库及答案全考点
- GB 17790-2008家用和类似用途空调器安装规范
评论
0/150
提交评论