java往指定的excel中写数据



package com.test; 


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 {  
        //创建一个EXCEL  
        Workbook wb = new HSSFWorkbook();  
        DataFormat format = wb.createDataFormat();  
        CellStyle style;  
        //创建一个SHEET  
        Sheet 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("test.xls");  
        wb.write(fileOut);  
        fileOut.close();  
  
  
    }  
  

0

package com.test; 


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 {  
        //创建一个EXCEL  
        Workbook wb = new HSSFWorkbook();  
        DataFormat format = wb.createDataFormat();  
        CellStyle style;  
        //创建一个SHEET  
        Sheet 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("test.xls");  
        wb.write(fileOut);  
        fileOut.close();  
  
  
    }  
  

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值