- 浏览: 464908 次
- 性别:
- 来自: 广州
文章分类
- 全部博客 (190)
- ExtJS (40)
- JavaScript (16)
- Java设计模式 (13)
- Html (10)
- Tomcat (9)
- myeclipse (5)
- JSP (5)
- CSS (10)
- JS/CSS (1)
- windows (12)
- Navicat For MySQL (1)
- struts2 (6)
- Spring2.0 (1)
- Java反射机制 (5)
- hibernate (9)
- java (17)
- B/SWeb完美打印方案 (1)
- 数据库相关 (4)
- java web报表 (1)
- 报表打印 (2)
- java泛型 (1)
- Servlet (2)
- SVN (7)
- Log4j (0)
- android (3)
最新评论
-
kation733:
斯蒂芬斯蒂芬多萨法阿凡达是否阿萨德
同时开启两个Tomcat -
BlueBing:
一路的夏天 写道楼主的代码会报错吧!在前面的时候String ...
使用Java反射机制遍历实体类的属性和类型 -
ying890:
非常感谢!
Ext JS中文乱码解决方案 -
zhylandroid:
谢谢了,对我很有帮助
DatabaseMetaData开发实务(上) -
wendy.wujing:
maxHttpHeaderSize="8192&qu ...
查看Tomcat配置的端口
14. 自定义颜色
HSSF:
- HSSFWorkbook wb = new HSSFWorkbook();
- HSSFSheet sheet = wb.createSheet();
- HSSFRow row = sheet.createRow((short) 0);
- HSSFCell cell = row.createCell((short) 0);
- cell.setCellValue("Default Palette");
- //apply some colors from the standard palette,
- // as in the previous examples.
- //we'll use red text on a lime background
- HSSFCellStyle style = wb.createCellStyle();
- style.setFillForegroundColor(HSSFColor.LIME.index);
- style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
- HSSFFont font = wb.createFont();
- font.setColor(HSSFColor.RED.index);
- style.setFont(font);
- cell.setCellStyle(style);
- //save with the default palette
- FileOutputStream out = new FileOutputStream("default_palette.xls");
- wb.write(out);
- out.close();
- //now, let's replace RED and LIME in the palette
- // with a more attractive combination
- // (lovingly borrowed from freebsd.org)
- cell.setCellValue("Modified Palette");
- //creating a custom palette for the workbook
- HSSFPalette palette = 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);
- //save with the modified palette
- // note that wherever we have previously used RED or LIME, the
- // new colors magically appear
- out = new FileOutputStream("modified_palette.xls");
- wb.write(out);
- out.close();
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet();
HSSFRow row = sheet.createRow((short) 0);
HSSFCell cell = row.createCell((short) 0);
cell.setCellValue("Default Palette");
//apply some colors from the standard palette,
// as in the previous examples.
//we'll use red text on a lime background
HSSFCellStyle style = wb.createCellStyle();
style.setFillForegroundColor(HSSFColor.LIME.index);
style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
HSSFFont font = wb.createFont();
font.setColor(HSSFColor.RED.index);
style.setFont(font);
cell.setCellStyle(style);
//save with the default palette
FileOutputStream out = new FileOutputStream("default_palette.xls");
wb.write(out);
out.close();
//now, let's replace RED and LIME in the palette
// with a more attractive combination
// (lovingly borrowed from freebsd.org)
cell.setCellValue("Modified Palette");
//creating a custom palette for the workbook
HSSFPalette palette = 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);
//save with the modified palette
// note that wherever we have previously used RED or LIME, the
// new colors magically appear
out = new FileOutputStream("modified_palette.xls");
wb.write(out);
out.close();
XSSF:
- XSSFWorkbook wb = new XSSFWorkbook();
- XSSFSheet sheet = wb.createSheet();
- XSSFRow row = sheet.createRow(0);
- XSSFCell cell = row.createCell( 0);
- cell.setCellValue("custom XSSF colors");
- XSSFCellStyle style1 = wb.createCellStyle();
- style1.setFillForegroundColor(new XSSFColor(new java.awt.Color(128, 0, 128)));
- style1.setFillPattern(CellStyle.SOLID_FOREGROUND);
XSSFWorkbook wb = new XSSFWorkbook();
XSSFSheet sheet = wb.createSheet();
XSSFRow row = sheet.createRow(0);
XSSFCell cell = row.createCell( 0);
cell.setCellValue("custom XSSF colors");
XSSFCellStyle style1 = wb.createCellStyle();
style1.setFillForegroundColor(new XSSFColor(new java.awt.Color(128, 0, 128)));
style1.setFillPattern(CellStyle.SOLID_FOREGROUND);
15. 读取和重写工作薄
- InputStream inp = new FileInputStream("workbook.xls");
- //InputStream inp = new FileInputStream("workbook.xlsx");
- Workbook wb = WorkbookFactory.create(inp);
- Sheet sheet = wb.getSheetAt(0);
- Row row = sheet.getRow(2);
- Cell cell = row.getCell(3);
- if (cell == null)
- cell = row.createCell(3);
- cell.setCellType(Cell.CELL_TYPE_STRING);
- cell.setCellValue("a test");
- // Write the output to a file
- FileOutputStream fileOut = new FileOutputStream("workbook.xls");
- wb.write(fileOut);
- fileOut.close();
InputStream inp = new FileInputStream("workbook.xls");
//InputStream inp = new FileInputStream("workbook.xlsx");
Workbook wb = WorkbookFactory.create(inp);
Sheet sheet = wb.getSheetAt(0);
Row row = sheet.getRow(2);
Cell cell = row.getCell(3);
if (cell == null)
cell = row.createCell(3);
cell.setCellType(Cell.CELL_TYPE_STRING);
cell.setCellValue("a test");
// Write the output to a file
FileOutputStream fileOut = new FileOutputStream("workbook.xls");
wb.write(fileOut);
fileOut.close();
16. 单元格内容换行
- Workbook wb = new XSSFWorkbook(); //or new HSSFWorkbook();
- Sheet sheet = wb.createSheet();
- Row row = sheet.createRow(2);
- Cell cell = row.createCell(2);
- cell.setCellValue("Use \n with word wrap on to create a new line");
- //to enable newlines you need set a cell styles with wrap=true
- CellStyle cs = wb.createCellStyle();
- cs.setWrapText(true);
- cell.setCellStyle(cs);
- //increase row height to accomodate two lines of text
- row.setHeightInPoints((2*sheet.getDefaultRowHeightInPoints()));
- //adjust column width to fit the content
- sheet.autoSizeColumn((short)2);
- FileOutputStream fileOut = new FileOutputStream("ooxml-newlines.xlsx");
- wb.write(fileOut);
- fileOut.close();
Workbook wb = new XSSFWorkbook(); //or new HSSFWorkbook();
Sheet sheet = wb.createSheet();
Row row = sheet.createRow(2);
Cell cell = row.createCell(2);
cell.setCellValue("Use \n with word wrap on to create a new line");
//to enable newlines you need set a cell styles with wrap=true
CellStyle cs = wb.createCellStyle();
cs.setWrapText(true);
cell.setCellStyle(cs);
//increase row height to accomodate two lines of text
row.setHeightInPoints((2*sheet.getDefaultRowHeightInPoints()));
//adjust column width to fit the content
sheet.autoSizeColumn((short)2);
FileOutputStream fileOut = new FileOutputStream("ooxml-newlines.xlsx");
wb.write(fileOut);
fileOut.close();
换行的步骤:
- 在需要换行的地方加上\n cell.setCellValue("Use \n with word wrap on to create a new line");
- 设置行高 row.setHeightInPoints((2*sheet.getDefaultRowHeightInPoints()));
- 设置列只适应宽度 sheet.autoSizeColumn((short)2);
17. 数据格式化
- public void dataFormat() throws Exception{
- Workbook wb = new HSSFWorkbook();
- Sheet sheet = wb.createSheet("format sheet");
- CellStyle style;
- DataFormat format = wb.createDataFormat();
- Row row;
- Cell cell;
- short rowNum = 0;
- short colNum = 0;
- row = sheet.createRow(rowNum++);
- cell = row.createCell(colNum);
- cell.setCellValue(11111.25);
- style = wb.createCellStyle();
- style.setDataFormat(format.getFormat("0.0"));
- cell.setCellStyle(style);
- row = sheet.createRow(rowNum++);
- cell = row.createCell(colNum);
- cell.setCellValue(1111.25);
- style = wb.createCellStyle();
- style.setDataFormat(format.getFormat("#,###.0000"));
- cell.setCellStyle(style);
- FileOutputStream fileOut = new FileOutputStream("workbook.xls");
- wb.write(fileOut);
- fileOut.close();
- }
public void dataFormat() throws Exception{
Workbook wb = new HSSFWorkbook();
Sheet sheet = wb.createSheet("format sheet");
CellStyle style;
DataFormat format = wb.createDataFormat();
Row row;
Cell cell;
short rowNum = 0;
short colNum = 0;
row = sheet.createRow(rowNum++);
cell = row.createCell(colNum);
cell.setCellValue(11111.25);
style = wb.createCellStyle();
style.setDataFormat(format.getFormat("0.0"));
cell.setCellStyle(style);
row = sheet.createRow(rowNum++);
cell = row.createCell(colNum);
cell.setCellValue(1111.25);
style = wb.createCellStyle();
style.setDataFormat(format.getFormat("#,###.0000"));
cell.setCellStyle(style);
FileOutputStream fileOut = new FileOutputStream("workbook.xls");
wb.write(fileOut);
fileOut.close();
}
18. 设置打印区域
- Workbook wb = new HSSFWorkbook();
- Sheet sheet = wb.createSheet("Sheet1");
- //sets the print area for the first sheet
- wb.setPrintArea(0, "$A$1:$C$2");
- //Alternatively:
- wb.setPrintArea(
- 0, //sheet index
- 0, //start column
- 1, //end column
- 0, //start row
- 0 //end row
- );
- FileOutputStream fileOut = new FileOutputStream("workbook.xls");
- wb.write(fileOut);
- fileOut.close();
Workbook wb = new HSSFWorkbook();
Sheet sheet = wb.createSheet("Sheet1");
//sets the print area for the first sheet
wb.setPrintArea(0, "$A$1:$C$2");
//Alternatively:
wb.setPrintArea(
0, //sheet index
0, //start column
1, //end column
0, //start row
0 //end row
);
FileOutputStream fileOut = new FileOutputStream("workbook.xls");
wb.write(fileOut);
fileOut.close();
19. 设置页脚
- HSSFWorkbook wb = new HSSFWorkbook();
- HSSFSheet sheet = wb.createSheet("format sheet");
- HSSFFooter footer = sheet.getFooter();
- footer.setRight( "Page " + HSSFFooter.page() + " of " + HSSFFooter.numPages() );
- // Create various cells and rows for spreadsheet.
- FileOutputStream fileOut = new FileOutputStream("workbook.xls");
- wb.write(fileOut);
- fileOut.close();
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet("format sheet");
HSSFFooter footer = sheet.getFooter();
footer.setRight( "Page " + HSSFFooter.page() + " of " + HSSFFooter.numPages() );
// Create various cells and rows for spreadsheet.
FileOutputStream fileOut = new FileOutputStream("workbook.xls");
wb.write(fileOut);
fileOut.close();
备注:只有在打印预览的时候页脚才显示出来。
20. 选中一个Sheet
- Workbook wb = new HSSFWorkbook();
- Sheet sheet = wb.createSheet("row sheet");
- sheet.setSelected(true);
Workbook wb = new HSSFWorkbook();
Sheet sheet = wb.createSheet("row sheet");
sheet.setSelected(true);
21. 设置方法倍率
- Workbook wb = new HSSFWorkbook();
- Sheet sheet1 = wb.createSheet("new sheet");
- sheet1.setZoom(3,4); // 75 percent magnification
Workbook wb = new HSSFWorkbook();
Sheet sheet1 = wb.createSheet("new sheet");
sheet1.setZoom(3,4); // 75 percent magnification
Zoom是一个分数,例如如果方法75%,那么分子为3,分母为4。
22. 设置打印时的页宽和页高
- 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("workbook.xls");
- wb.write(fileOut);
- fileOut.close();
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("workbook.xls");
wb.write(fileOut);
fileOut.close();
23. 移动行
- public void shiftRow() throws Exception{
- Workbook wb = new HSSFWorkbook();
- Sheet sheet = wb.createSheet("row sheet");
- for(int i=0;i<20;i++){
- Row row=sheet.createRow(i);
- Cell cell=row.createCell(0);
- cell.setCellValue(""+i);
- }
- // Shift rows 6 - 11 on the spreadsheet to the top (rows 0 - 5)
- //把第6-11行向上移动5行
- sheet.shiftRows(5, 10,-5);
- FileOutputStream fileOut = new FileOutputStream("workbook.xls");
- wb.write(fileOut);
- fileOut.close();
- }
发表评论
-
Errors running builder 'JavaScript Validator' on project '......'
2013-02-22 12:14 971在编译java工程时,如果出现 “Errors occur ... -
list,set,map,数组间的相互转换
2012-09-29 13:53 1009list,set,map,数组间的相互转换 ... -
POI学习笔记(二)
2010-06-29 15:12 49987. 设置单元格的边框 Java代码 ... -
POI学习笔记(一)
2010-06-29 15:10 7335项目中经常要解析和生成Excel文件,最常用的开源组件有poi ... -
web.xml 配置详解
2010-05-27 11:07 1090在项目中总会遇到一些关于加载的优先级问题,近期也同样遇到过类似 ... -
如何调用hh.exe或者别的命令行的方式,在已经打开了的chm中,定位到指定页面?
2010-05-25 11:55 2122hh.exe mk:@MSITStore:F:\\buyord ... -
如何用一个按钮打开chm的帮助文件
2010-05-25 10:26 2082如果是在windows下运行的话,可以用Runtime的get ... -
java文件或jsp、xml文件注释
2010-01-13 16:29 1793Java用Shift+Ctrl+C(单行注释)没问题,Jsp页 ... -
对Java Serializable(序列化)的理解和总结
2010-01-12 09:05 10301、序列化是干什么的? ... -
基本数据类型转换
2010-01-06 09:42 1018int -> String int i=12345;S ... -
配置JDK
2009-12-18 15:07 1353下载好的JDK是一个可执 ... -
一个java处理JSON格式数据的通用类
2009-12-10 16:50 1201一个java处理JSON格式数据的通用类 进入需要在项目中用 ... -
JAVA注解
2009-12-04 08:33 2667java 注解 可以使用在类,方法,全局变量,局部变量作用: ... -
java对象排序
2009-11-18 09:46 1292Java集合对象排序测试 Java API针对集合类型排 ... -
java中对于复杂对象排序的模型及其实现
2009-11-18 09:41 1125排序是编程中经常要碰到的问题,如果只是一般的数据库数据那么我们 ... -
java中对于复杂对象排序的模型及其实现
2009-10-23 15:26 1414排序是编程中经常要碰到的问题,如果只是一般的数据库数据那么我们 ...
相关推荐
POI学习笔记第二版更详细的POI学习笔记第二版更详细的
POI学习笔记 POI中文教程(简单的)
POI的学习笔记,总结的还行。希望大家会喜欢
百度地图学习笔记,有代码。图层 定位 地理编码 poi搜索
OA (ssh) 基本实现(poi 生成 Excel , struts2动态下载 mysql数据库文件) 学习笔记(含源代码) 借鉴 风中叶 老师的视频,写的文章,代码比较详实。 说了很多我的看法,和思考,做了充分的日志
[摘要]:本文通过整理与VC维相关的重要概念,对分(Dichotomy)、增长函数(Growth Function)、打散(Shatter)、Break Poi
Spring Boot 学习笔记 基础篇 #提升篇 15.SpringBoot之集成Shiro 16.SpringBoot之使用mybatis-generator自动生成代码 17.SpringBoot之使用lombok编码 18.SpringBoot之初始化数据 19.SpringBoot之使用POI开发Excel...
java学习过程中的随手笔记,包括常见的文件选择和保存窗口的实现,poi的excel处理
非常值得学习的资源,结合笔记更快上手。 有poi报表生成,访问分析等技术, 非常值得学习的资源,结合笔记更快上手。 有poi报表生成,访问分析等技术,