JAVA用POI读取和创建2003和2007版本Excel完美示例

JAVA用POI读取和创建2003和2007版本Excel完美示例


2024年7月2日发(作者:)

import ;

import putStream;

import tFoundException;

import tputStream;

import ption;

import lFormat;

import DateFormat;

import ;

import List;

import ;

import ll;

import llStyle;

import taFormat;

import teUtil;

import nt;

import w;

import eet;

import rkbook;

import lor;

import ll;

import llStyle;

import nt;

import w;

import eet;

import rkbook;

/**

* 可以从/ 这里下载到POI的jar包 POI

创建和读取2003-2007版本Excel文件

*

*/

public class CreatAndReadExcel {

public static void main(String[] args) throws Exception {

creat2003Excel();// 创建2007版Excel文件

creat2007Excel();// 创建2003版Excel文件

//读取2003Excel文件

String path2003 = perty("")

+ perty("tor") + "style_";// 获取项目文件路径

+2003版文件名

n("路径:" + path2003);

File f2003 = new File(path2003);

try {

readExcel(f2003);

} catch (IOException e) {

// TODO Auto-generated catch block

tackTrace();

}

//读取2007Excel文件

String path2007 = perty("")

+ perty("tor") + "style_";// 获取项目文件路径

+2007版文件名

n("路径:" + path2007);

File f2007 = new File(path2007);

try {

readExcel(f2007);

} catch (IOException e) {

// TODO Auto-generated catch block

tackTrace();

}

}

/**

* 创建2007版Excel文件

*

* @throws FileNotFoundException

* @throws IOException

*/

private static void creat2007Excel() throws FileNotFoundException,

IOException {

// HSSFWorkbook workBook = new HSSFWorkbook();// 创建 一个excel文档对

XSSFWorkbook workBook = new XSSFWorkbook();

XSSFSheet sheet = Sheet();// 创建一个工作薄对象

umnWidth(1, 10000);// 设置第二列的宽度为

XSSFRow row = Row(1);// 创建一个行对象

ghtInPoints(23);// 设置行高23像素

XSSFCellStyle style = CellStyle();// 创建样式对象

// 设置字体

XSSFFont font = Font();// 创建字体对象

tHeightInPoints((short) 15);// 设置字体大小

dweight(IGHT_BOLD);// 设置粗体

tName("黑体");// 设置为黑体字

t(font);// 将字体加入到样式对象

// 设置对齐方式

gnment(_CENTER_SELECTION);// 水平居中

ticalAlignment(AL_CENTER);// 垂直居中

// 设置边框

derTop(_THICK);// 顶部边框粗线

BorderColor();// 设置为红色

derBottom(_DOUBLE);// 底部边框双线

derLeft(_MEDIUM);// 左边边框

derRight(_MEDIUM);// 右边边框

// 格式化日期

aFormat(ltinFormat("m/d/yy h:mm"));

XSSFCell cell = Cell(1);// 创建单元格

lValue(new Date());// 写入当前日期

lStyle(style);// 应用样式对象

// 文件输出流

FileOutputStream os = new FileOutputStream("style_");

(os);// 将文档对象写入文件输出流

();// 关闭文件输出流

n("创建成功 office 2007 excel");

}

/**

* 创建2003版本的Excel文件

*/

private static void creat2003Excel() throws FileNotFoundException,

IOException {

HSSFWorkbook workBook = new HSSFWorkbook();// 创建 一个excel文档对象

HSSFSheet sheet = Sheet();// 创建一个工作薄对象

umnWidth(1, 10000);// 设置第二列的宽度为

HSSFRow row = Row(1);// 创建一个行对象

ghtInPoints(23);// 设置行高23像素

HSSFCellStyle style = CellStyle();// 创建样式对象

// 设置字体

HSSFFont font = Font();// 创建字体对象

tHeightInPoints((short) 15);// 设置字体大小

dweight(IGHT_BOLD);// 设置粗体

tName("黑体");// 设置为黑体字

t(font);// 将字体加入到样式对象

// 设置对齐方式

gnment(_CENTER_SELECTION);// 水平居中

ticalAlignment(AL_CENTER);// 垂直居中

// 设置边框

derTop(_THICK);// 顶部边框粗线

BorderColor();// 设置为红色

derBottom(_DOUBLE);// 底部边框双线

derLeft(_MEDIUM);// 左边边框

derRight(_MEDIUM);// 右边边框

// 格式化日期

aFormat(ltinFormat("m/d/yy h:mm"));

HSSFCell cell = Cell(1);// 创建单元格

lValue(new Date());// 写入当前日期

lStyle(style);// 应用样式对象

// 文件输出流

FileOutputStream os = new FileOutputStream("style_");

(os);// 将文档对象写入文件输出流

();// 关闭文件输出流

n("创建成功 office 2003 excel");

}

/**

* 对外提供读取excel 的方法

*/

public static List> readExcel(File file) throws IOException {

String fileName = e();

String extension = dexOf(".") == -1 ? "" : fileName

.substring(dexOf(".") + 1);

if ("xls".equals(extension)) {

return read2003Excel(file);

} else if ("xlsx".equals(extension)) {

return read2007Excel(file);

} else {

throw new IOException("不支持的文件类型");

}

}

/**

* 读取 office 2003 excel

*

* @throws IOException

* @throws FileNotFoundException

*/

private static List> read2003Excel(File file)

throws IOException {

List> list = new LinkedList>();

HSSFWorkbook hwb = new HSSFWorkbook(new FileInputStream(file));

HSSFSheet sheet = etAt(0);

Object value = null;

HSSFRow row = null;

HSSFCell cell = null;

n("读取office 2003 excel内容如下:");

for (int i = stRowNum(); i <= sheet

.getPhysicalNumberOfRows(); i++) {

row = (i);

if (row == null) {

continue;

}

List linked = new LinkedList();

for (int j = stCellNum(); j <= tCellNum(); j++) {

cell = l(j);

if (cell == null) {

continue;

}

DecimalFormat df = new DecimalFormat("0");// 格式化 number String

// 字符

SimpleDateFormat sdf = new SimpleDateFormat(

"yyyy-MM-dd HH:mm:ss");// 格式化日期字符串

DecimalFormat nf = new DecimalFormat("0.00");// 格式化数字

switch (lType()) {

case _TYPE_STRING:

// n(i + "行" + j + " 列 is String type");

value = ingCellValue();

(" " + value + " ");

break;

case _TYPE_NUMERIC:

// n(i + "行" + j

// + " 列 is Number type ; DateFormt:"

// + lStyle().getDataFormatString());

if ("@".equals(lStyle().getDataFormatString())) {

value = (ericCellValue());

} else if ("General".equals(lStyle()

.getDataFormatString())) {

value = (ericCellValue());

} else {

value = (aDate(cell

.getNumericCellValue()));

}

(" " + value + " ");

break;

case _TYPE_BOOLEAN:

// n(i + "行" + j + " 列 is Boolean type");

value = leanCellValue();

(" " + value + " ");

break;

case _TYPE_BLANK:

// n(i + "行" + j + " 列 is Blank type");

value = "";

(" " + value + " ");

break;

default:

// n(i + "行" + j + " 列 is default type");

value = ng();

(" " + value + " ");

}

if (value == null || "".equals(value)) {

continue;

}

(value);

}

n("");

(linked);

}

return list;

}

/**

* 读取Office 2007 excel

*/

private static List> read2007Excel(File file)

throws IOException {

List> list = new LinkedList>();

// String path = perty("") +

// perty("tor")+"";

// n("路径:"+path);

// 构造 XSSFWorkbook 对象,strPath 传入文件路径

XSSFWorkbook xwb = new XSSFWorkbook(new FileInputStream(file));

// 读取第一章表格内容

XSSFSheet sheet = etAt(0);

Object value = null;

XSSFRow row = null;

XSSFCell cell = null;

n("读取office 2007 excel内容如下:");

for (int i = stRowNum(); i <= sheet

.getPhysicalNumberOfRows(); i++) {

row = (i);

if (row == null) {

continue;

}

List linked = new LinkedList();

for (int j = stCellNum(); j <= tCellNum(); j++) {

cell = l(j);

if (cell == null) {

continue;

}

DecimalFormat df = new DecimalFormat("0");// 格式化 number String

// 字符

SimpleDateFormat sdf = new SimpleDateFormat(

"yyyy-MM-dd HH:mm:ss");// 格式化日期字符串

DecimalFormat nf = new DecimalFormat("0.00");// 格式化数字

switch (lType()) {

case _TYPE_STRING:

// n(i + "行" + j + " 列 is String type");

value = ingCellValue();

(" " + value + " ");

break;

case _TYPE_NUMERIC:

// n(i + "行" + j

// + " 列 is Number type ; DateFormt:"

// + lStyle().getDataFormatString());

if ("@".equals(lStyle().getDataFormatString())) {

value = (ericCellValue());

} else if ("General".equals(lStyle()

.getDataFormatString())) {

value = (ericCellValue());

} else {

value = (aDate(cell

.getNumericCellValue()));

}

(" " + value + " ");

break;

case _TYPE_BOOLEAN:

// n(i + "行" + j + " 列 is Boolean type");

value = leanCellValue();

(" " + value + " ");

break;

case _TYPE_BLANK:

// n(i + "行" + j + " 列 is Blank type");

value = "";

// n(value);

break;

default:

// n(i + "行" + j + " 列 is default type");

value = ng();

(" " + value + " ");

}

if (value == null || "".equals(value)) {

continue;

}

(value);

}

n("");

(linked);

}

return list;

}

}


发布者:admin,转转请注明出处:http://www.yc00.com/web/1719929902a2759284.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信