您的位置:首页 > 其它

央视名嘴经典语录

2008-09-11 19:53 232 查看
package cn.yzb.dome.excel;import java.io.FileOutputStream;import java.text.SimpleDateFormat;import java.util.Date;import org.apache.poi.hssf.usermodel.HSSFWorkbook;import org.apache.poi.ss.usermodel.Cell;import org.apache.poi.ss.usermodel.CellStyle;import org.apache.poi.ss.usermodel.DataFormat;import org.apache.poi.ss.usermodel.Row;import org.apache.poi.ss.usermodel.Sheet;import org.apache.poi.ss.usermodel.Workbook;import org.apache.poi.ss.util.CellRangeAddress;public class poiCreate {/** * @param args */public static void main(String[] args) throws Exception {// 创建一个EXCELWorkbook wb = new HSSFWorkbook();DataFormat format = wb.createDataFormat();CellStyle style;// 创建一个SHEETSheet sheet1 = wb.createSheet("产品清单");String[] title = { "编号", "产品名称", "产品价格", "产品数量", "生产日期", "产地", "是否出口" };int i = 0;// 创建一行Row row = sheet1.createRow((short) 0);// 填充标题for (String s : title) {Cell cell = row.createCell(i);cell.setCellValue(s);i++;}Row row1 = sheet1.createRow((short) 1);// 下面是填充数据row1.createCell(0).setCellValue(20071001);row1.createCell(1).setCellValue("金鸽瓜子");// 创建一个单元格子Cell cell2 = row1.createCell(2);// 填充产品价格cell2.setCellValue(2.45);style = wb.createCellStyle();style.setDataFormat(format.getFormat("#.##"));// 设定样式cell2.setCellStyle(style);// 填充产品数量row1.createCell(3).setCellValue(200);/** * 定义显示日期的公共格式 如:yyyy-MM-dd hh:mm * */SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");String newdate = sdf.format(new Date());// 填充出产日期row1.createCell(4).setCellValue(newdate);row1.createCell(5).setCellValue("陕西西安");/** * 显示布尔值 * */row1.createCell(6).setCellValue(true);/** * 合并单元格 通过writablesheet.mergeCells(int x,int y,int m,int n);来实现的 * 表示将first row, last row,first column,last column * * */Row row2 = sheet1.createRow((short) 2);Cell cell3 = row2.createCell((short) 0);cell3.setCellValue("合并了三个单元格");sheet1.addMergedRegion(new CellRangeAddress(2, 2, 0, 2));FileOutputStream fileOut = new FileOutputStream("d:\\test.xls");wb.write(fileOut);fileOut.close();}}

本文出自 “Java-Baby” 博客,请务必保留此出处http://yzbxcf.blog.51cto.com/4142491/1201992
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: