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

这篇博客介绍了如何使用Apache POI库在Java中创建Excel工作簿和工作表,并将数据写入单元格。通过示例代码展示了如何创建一个新的Excel文件,创建多个工作表,以及设置单元格的数值和字符串内容。
摘要由CSDN通过智能技术生成

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

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值