您的位置:首页 > 其它

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();

}}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  poi