用户注册



邮箱:

密码:

用户登录


邮箱:

密码:
记住登录一个月忘记密码?

发表随想


还能输入:200字
云代码 - java代码库

POI操作excel表格(建立工作薄、创建工作表、将数据填充到单元格中)

2013-01-16 作者: 小蜜锋举报

[java]代码库

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package excel;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import java.io.FileInputStream;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFRow;
/**
 *
 * @author Administrator
 */
public class ExcelUtil
{
    public String filePath = "e:\\workbook.xls";
    public void newWordBook()
    {
        HSSFWorkbook wb = new HSSFWorkbook();
        try
        {
            FileOutputStream fileOut = new FileOutputStream (filePath);
            wb.write (fileOut);
            fileOut.close();
        }
        catch (FileNotFoundException ex)
        {
            System.out.println (ex.getMessage() );
        }
        catch (IOException ex)
        {
            System.out.println (ex.getMessage() );
        }
    }
    /**
     * 创建空白文件
     */
    public void newSheet()
    {
        HSSFWorkbook wb = new HSSFWorkbook();
        wb.createSheet ("第一页");
        wb.createSheet ("第二页");
        try
        {
            FileOutputStream fileOut = new FileOutputStream (filePath);
            wb.write (fileOut);
            fileOut.close();
        }
        catch (FileNotFoundException ex)
        {
            System.out.println (ex.getMessage() );
        }
        catch (IOException ex)
        {
            System.out.println (ex.getMessage() );
        }
    }
    private void saveWorkBook (HSSFWorkbook wb)
    {
        try
        {
            FileOutputStream fileOut = new FileOutputStream (filePath);
            wb.write (fileOut);
            fileOut.close();
        }
        catch (FileNotFoundException ex)
        {
            System.out.println (ex.getMessage() );
        }
        catch (IOException ex)
        {
            System.out.println (ex.getMessage() );
        }
    }
    private HSSFWorkbook getWorkBook (String filePath)
    {
        try
        {
            FileInputStream fileIn = new FileInputStream (filePath);
            HSSFWorkbook wb = new HSSFWorkbook (fileIn);
            fileIn.close();
            return wb;
        }
        catch (IOException ex)
        {
            System.out.println (ex.getMessage() );
            return null;
        }
    }
    private HSSFCell getCell (HSSFSheet sheet, int rowIndex, short columnIndex)
    {
        HSSFRow row = sheet.getRow (rowIndex);
        if (row == null)
        {
            row = sheet.createRow (rowIndex);
        }
        HSSFCell cell = row.getCell (columnIndex);
        if (cell == null)
        {
            cell = row.createCell ( (short) columnIndex);
        }
        return cell;
    }
    /**
     * 写数据
     * @param file
     */
    public void writeData (String file)
    {
        //创建工作薄
        HSSFWorkbook wb = getWorkBook (file);
        if (wb == null)
        {
            return;
        }
        //获取工作表
        HSSFSheet sheet = wb.getSheetAt (0);
        if (sheet == null)
        {
            sheet = wb.createSheet ("第一页");
        }
        HSSFCell cell = getCell (sheet, 0, (short) 0);
        //数值
        cell.setCellValue (123);
        //字符串
        HSSFRichTextString str = new HSSFRichTextString ("你好");
        cell = getCell (sheet, 0, (short) 1);
        cell.setCellValue (str);
        //保存
        saveWorkBook (wb);
    }
    public static void main (String[] args)
    {
        ExcelUtil excel = new ExcelUtil();
        excel.writeData (excel.filePath);
    }
}


网友评论    (发表评论)


发表评论:

评论须知:

  • 1、评论每次加2分,每天上限为30;
  • 2、请文明用语,共同创建干净的技术交流环境;
  • 3、若被发现提交非法信息,评论将会被删除,并且给予扣分处理,严重者给予封号处理;
  • 4、请勿发布广告信息或其他无关评论,否则将会删除评论并扣分,严重者给予封号处理。


扫码下载

加载中,请稍后...

输入口令后可复制整站源码

加载中,请稍后...