转载至:
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();
}
}
分享到:
相关推荐
使用poi将xls格式的Excel转为xlsx,文件中的所有样式都可以完美转换,例如合并栏、背景和单元格样式等。Java源码
poi生成wordpoi生成wordpoi生成wordpoi生成wordpoi生成wordpoi生成wordpoi生成wordpoi生成wordpoi生成word
poi转换xls到xlsx
Java POI 生成Word文档,支持图片插入,关键是修改XML部分,本人已测试通过。
POI 创建 xls 或 xlsx 格式excel所需要的所有jar, 包括简化文件操作的FileUtils类所在的包
poi生成报表,倒进去直接能运行,新人很值得参考,附带poi最新的包
1.java使用poi生成excel插入图表曲线 2.demo包含完成jar包 3.主要用途是过滤数据自动生成曲线图
很实用的帮助文档,分门别类的解决不少实际问题
技术使用poi,实际功能为html文件转换成word, 生成pdf格式合同,html生成图片,生成html格式合同,下载解压缩即可运行
Excel poi 支持xls 和xlsx,支持03 和07 版表格,excel 通用工具类及使用参考
生成XLS文件是使用POI(Apache POI)库来创建Excel文件的过程。POI是一个开源项目,由Apache基金会维护。它提供了一系列的Java API,用于读写MS Office文件,包括Excel、Word、PowerPoint等。 在生成XLS文件时,...
最近在做一个出卷系统,需要通过试卷模板从数据库中抽取题目,然后按照模板的样式生成完整的试卷,包括试卷格式的排版和图片的写入。这是用POI模板生成word文档。里面包括完整代码和所需jar包。
使用poi生成Excel所需要的jar包,poi-3.9.jar
将assets中.xml中的doc文件中,就会生成word文档和excel文档
poi 导入导出实例例子poi 导入导出实例例子
struts2 poi 导入xls xlsx绝对兼容,直接导入myeclipse 使用
java使用poi读取xls格式和xlsx格式的excel文件
本篇文章主要介绍了利用Java Apache POI 生成Word文档示例代码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
博文链接:https://weiweichen1985.iteye.com/blog/204158
poi 生成word 所需jar,dom4j-1.6.1.jar ooxml-schemas-1.1.jar poi-3.12-20150511.jar poi-ooxml-3.12-20150511.jar poi-scratchpad-3.12-20150511.jar xmlbeans-2.4.0.jar