poi的使用模型
2017-12-24 22:34
79 查看
package cn.test.demo; import java.io.FileNotFoundException; 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.ss.util.CellRangeAddress; /** * POI样式 * @author Administrator * */ public class Test2 { public static void main(String[] args) throws FileNotFoundException, IOException { HSSFWorkbook book=new HSSFWorkbook(); HSSFSheet sheet = book.createSheet("采购单"); /****** 画框线 ******/ int rowCount=12; //创建四周带边框的样式 (内容部分) HSSFCellStyle style_content = book.createCellStyle(); style_content.setBorderBottom(HSSFCellStyle.BORDER_THIN);//设置底部边框 style_content.setBorderTop(HSSFCellStyle.BORDER_THIN);//设置顶部边框 style_content.setBorderLeft(HSSFCellStyle.BORDER_THIN);//设置左侧边框 style_content.setBorderRight(HSSFCellStyle.BORDER_THIN);//设置右侧边框 //循环创建内容单元格 for(int i=2;i<rowCount;i++){ HSSFRow row = sheet.createRow(i);//循环创建内容行 for(int j=0;j<4;j++){ HSSFCell cell = row.createCell(j);//循环创建单元格 cell.setCellStyle(style_content);//设置单元格样式 } } /***** 合并单元格 *****/ //起始行,截止行,起始列,截止列 sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 3)); sheet.addMergedRegion(new CellRangeAddress(2, 2, 1, 3)); sheet.addMergedRegion(new CellRangeAddress(7, 7, 0, 3)); /***** 设置单元格的值 *****/ sheet.createRow(0).createCell(0).setCellValue("采购单"); sheet.getRow(2).getCell(0).setCellValue("供应商"); sheet.getRow(3).getCell(0).setCellValue("下单日期"); sheet.getRow(4).getCell(0).setCellValue("审核日期"); sheet.getRow(5).getCell(0).setCellValue("确认日期"); sheet.getRow(6).getCell(0).setCellValue("结束日期"); sheet.getRow(3).getCell(2).setCellValue("经办人"); sheet.getRow(4).getCell(2).setCellValue("经办人"); sheet.getRow(5).getCell(2).setCellValue("经办人"); sheet.getRow(6).getCell(2).setCellValue("经办人"); sheet.getRow(7).getCell(0).setCellValue("订单明细"); sheet.getRow(8).getCell(0).setCellValue("商品名称"); sheet.getRow(8).getCell(1).setCellValue("价格"); sheet.getRow(8).getCell(2).setCellValue("数量"); sheet.getRow(8).getCell(3).setCellValue("金额"); sheet.getRow(rowCount-1).getCell(0).setCellValue("合计"); /**** 设置行高列宽 ******/ //设置列宽 for(int i=0;i<4;i++){ sheet.setColumnWidth(i, 5000); } //设置行高 for(int i=2;i<rowCount;i++){ HSSFRow row = sheet.getRow(i); row.setHeight((short) 500);//设置行高 } //设置标题行高度 sheet.getRow(0).setHeight((short) 1000); /****** 设置对齐方式 ******/ style_content.setAlignment(HSSFCellStyle.ALIGN_CENTER);//水平居中 style_content.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);//垂直居中 //创建字体(内容) HSSFFont font = book.createFont(); font.setFontName("宋体"); font.setFontHeightInPoints((short) 11); style_content.setFont(font);//设置字体 //设置单元格为文本格式 XSSFCellStyle cell_context = book.createCellStyle(); XSSFDataFormat format = book.createDataFormat(); cell_context.setDataFormat(format.getFormat("@")); //设置标题的样式 HSSFCellStyle style_title = book.createCellStyle(); style_title.setAlignment(HSSFCellStyle.ALIGN_CENTER); style_title.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); HSSFFont font_title = book.createFont();//标题所用字体 font_title.setFontName("黑体"); font_title.setFontHeightInPoints((short) 18); font_title.setBold(true);//加粗 style_title.setFont(font_title);//设置标题样式字体 sheet.getRow(0).getCell(0).setCellStyle(style_title);//设置标题样式 /***** 设置日期格式 *****/ HSSFDataFormat dataFormat = book.createDataFormat();//创建数据格式 HSSFCellStyle style_date = book.createCellStyle();//日期样式 style_date.cloneStyleFrom(style_content);//克隆样式 style_date.setDataFormat(dataFormat.getFormat("yyyy-MM-dd hh:mm")); for(int i=3;i<7;i++){ sheet.getRow(i).getCell(1).setCellStyle(style_date);//设置日期格式 } sheet.getRow(3).getCell(1).setCellValue(new Date()); book.write(new FileOutputStream("d:\\test2.xls"));
//如果不传下载路径,可以使用一些代码 response.setContentType("application/vnd.ms-excel"); response.setHeader("Content-disposition", "attachment;filename=client_template.xlsx"); OutputStream ouputStream = response.getOutputStream(); book.write(ouputStream); ouputStream.flush();ouputStream.close(); }}
相关文章推荐
- 简单数据预测—使用Python训练回归模型并进行预测(转自蓝鲸网站分析博客)
- 使用Cesium.js加载3D模型
- Java使用POI实现数据导出excel报表
- Atitit 并发技术的选项 attilax总结 艾龙 著 1. 三大并发模型 1 2. 从可读性考虑 优先使用 并行工作者 多线程模式,不要使用异步流水线模式 2 2.1. 多线程模式方便全局
- Poi 使用其他页做为下拉数据源
- 【TensorFlow】Caffe模型转tensorflow模型并使用模型进行预测
- java 使用读写锁设计一个缓存模型
- python使用tensorflow保存、加载和使用模型的方法
- 使用POI操作excel(读取和创建)快速开发指南
- 如何使用POI解决word2007中字符替换问题
- POI使用
- 使用keras训练一个区分不同人声音的模型
- [转载]使用ADO.NET EntityFramework实体模型类
- 使用C++实现一套简单的状态机模型——实例
- 使用caffe训练并且测试一个自己的模型
- 计算机图形学中的经常使用模型
- 使用svm机器学习模型对抓取异常进行挖掘
- java使用POI实现excel文件的读取,兼容后缀名xls和xlsx
- C/S通信---服务器IO多路复用模型之select的使用
- java使用poi导入excel表处理小数点