poi的使用模型
来源:互联网 发布:知乎 发现 编辑:程序博客网 时间:2024/06/01 15:35
package cn.itcast.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);//设置字体//设置标题的样式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"));}}
阅读全文