Excel工具类

[b]介绍[/b]
Apache POI是Apache软件基金会的开放源码函数库,POI提供API给Java程式Microsoft Office格式档案读和写的功能。

[b]结论[/b]

HSSF - 提供读写Microsoft Excel XLS格式档案的功能。
XSSF - 提供读写Microsoft Excel OOXML XLSX格式档案的功能。
HWPF - 提供读写Microsoft Word DOC格式档案的功能。
HSLF - 提供读写Microsoft PowerPoint格式档案的功能。
HDGF - 提供读Microsoft Visio格式档案的功能。
HPBF - 提供读Microsoft Publisher格式档案的功能。
HSMF - 提供读Microsoft Outlook格式档案的功能。


package com.mypack.core.utils;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

/**
* <ul>
* Excel工具类处理
* </ul>
*
* @author liudong
*
*/
public class ExcelUtils {

/**
*
* <li>创建Excel工作薄</li>
*
*
* @return
*/
public static HSSFWorkbook createHSSFWorkbook() {
// 创建Workbook对象(这一个对象代表着对应的一个Excel文件)
HSSFWorkbook workbook = new HSSFWorkbook();

return workbook;
}

/**
*
* <li>创建sheet</li>
*
* @param sheetName
* sheet名称
* @return
*/
public static HSSFSheet createHSSFSheet(HSSFWorkbook workbook,
String sheetName) {
if (workbook == null) {
throw new IllegalArgumentException("parameter workbook is not null");
}
if (sheetName == null || sheetName.equals("")) {
throw new IllegalArgumentException(
"parameter sheetName is not null");
}
// 创建Sheet并给名字(表示Excel的一个Sheet)
return workbook.createSheet(sheetName);

}

/**
*
* <li>填充表单内容</li>
*
* @param titles
*
* @param rowIndex
* sheet的第rowIndex行
*/
public static void fillSheetContent(HSSFSheet sheet, String[] titles,
int rowIndex) {

if (sheet == null) {
throw new IllegalArgumentException("parameter sheet is not null");
}

if (titles == null) {
throw new IllegalArgumentException("parameter titles is not null");
}
// 创建每一行
HSSFRow row = sheet.createRow((short) rowIndex);
if (titles != null) {
for (int i = 0; i < titles.length; i++) {
// 创建每一列
HSSFCell cell = row.createCell((short) i);
cell.setCellValue(new HSSFRichTextString(titles[i]));
}
}
}

/**
*
* <li>保存Excel文件</li>
*
* @param filePath
* 文件路径
*/
public static void saveExcel(HSSFWorkbook workbook, String filePath) {
if (workbook == null) {
throw new IllegalArgumentException("parameter workbook is not null");
}

if (filePath == null || filePath.equals("")) {
throw new IllegalArgumentException("parameter filePath is not null");
}
OutputStream out = null;
try {
out = new FileOutputStream(new File(filePath));
workbook.write(out);
out.close();
} catch (FileNotFoundException e) {
throw new RuntimeException(e.getMessage(), e);
} catch (IOException e) {
throw new RuntimeException(e.getMessage(), e);
} finally {
if (out != null) {
try {
out.close();
} catch (IOException e) {
throw new RuntimeException(e.getMessage(), e);
}
}
}
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值