现在的位置: 首页 > 综合 > 正文

读写Excel2003文档

2013年06月26日 ⁄ 综合 ⁄ 共 3633字 ⁄ 字号 评论关闭
1.程序说明
1.1编程语言:Java
1.2 第三方库:Apache POI
Apache POI 官网:http://poi.apache.org/
1.3程序功能
使用Apache POI读写Microsoft Excel文件
1.4程序作者
Fans同学
2.程序源代码
package excel;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Iterator;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
/**
* 使用Apache POI读写Microsoft Excel
*
* @author Fans.Lei
*
*/

public class ExcelDemo {
/** Excel 文件要存放的位置,假定在c盘poi目录下 */
public static String filePath = "C:/poi/fans.xls";
public static String sheetName = "Fans同学1.0";
// 标题栏
private String[] titles = { "姓名", "性别", "班级 ", "专业", " 学历", " 学校", " 口号"," 备注" };
// 信息栏
private String[][] infos = {
{ "Fans同学", "男", "083", "软件工程", "本科", "武汉科技大学",
"软林至尊,Fans同盟。号令天下,莫敢不从。", " 雷文" },
{ "刺客", "男", "083", "软件工程", "本科", "武汉科技大学",
"图书馆,第二列后七行,司马非马,最后的刺客,专诸,绝。", "郑富强" } };
//入口函数
public static void main(String args[]) {
ExcelDemo excelDemo = new ExcelDemo();
excelDemo.createExcel();
excelDemo.readExcel();
}
// 创建excel文件
public void createExcel() {
try {
// 创建新的Excel 工作簿
HSSFWorkbook workbook = new HSSFWorkbook();
// 在Excel工作簿中建一工作表,其名为sheetName
HSSFSheet sheet = workbook.createSheet(sheetName);
// 标题栏样式
HSSFFont font = workbook.createFont();
font.setColor(HSSFFont.COLOR_RED);
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
HSSFCellStyle boldRed = workbook.createCellStyle();
boldRed.setFont(font);
// 在索引0的位置创建行(最顶端的行)
HSSFRow titleRow = sheet.createRow(0);
// 向标题栏写内容
for (int columnIndex = 0; columnIndex < titles.length; columnIndex++) {
HSSFCell cell = titleRow.createCell(columnIndex);
cell.setCellValue(titles[columnIndex]);
cell.setCellStyle(boldRed);
}
// 向信息栏写内容
for (int rowIndex = 0; rowIndex < infos.length; rowIndex++) {
HSSFRow infoRow = sheet.createRow(rowIndex + 1);
for (int colIndex = 0; colIndex < infos[rowIndex].length; colIndex++) {
HSSFCell cell = infoRow.createCell(colIndex);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setCellValue(infos[rowIndex][colIndex]);
}
}
// 新建一输出文件流
FileOutputStream fos = new FileOutputStream(filePath);
// 把相应的Excel 工作簿存盘
workbook.write(fos);
fos.flush();
// 操作结束,关闭文件
fos.close();
System.out.println(filePath + "已创建!");
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
// 读取excel文件
public void readExcel() {
try {
// 创建对Excel工作簿文件的引用
HSSFWorkbook workbook = new HSSFWorkbook(new FileInputStream(
filePath));
// 创建对工作表的引用。
HSSFSheet sheet = workbook.getSheet(sheetName);
Iterator<Row> row = sheet.rowIterator();
while (row.hasNext()) {
Row curRow = row.next();
Iterator<Cell> cell = curRow.cellIterator();
while (cell.hasNext()) {
String cellValue = cell.next().getStringCellValue();
System.out.print(cellValue + "\t");
}
System.out.println();
}
} catch (Exception e) {
System.out.println(e.getMessage());
e.printStackTrace();
}
}
// 读取excel文件
public void readExcel2() {
try {
// 创建对Excel工作簿文件的引用
HSSFWorkbook workbook = new HSSFWorkbook(new FileInputStream(filePath));
// 创建对工作表的引用。
HSSFSheet sheet = workbook.getSheet(sheetName);
// 在索引0的位置创建行(最顶端的行)
HSSFRow titleRow = sheet.getRow(0);
for (int index = 0; index < titles.length; index++) {
// 在索引0的位置创建单元格(左上端)
HSSFCell cell = titleRow.getCell(index);
if (cell != null) {
System.out.println(cell.getStringCellValue());
}
}
for (int rowIndex = 0; rowIndex < infos.length; rowIndex++) {
HSSFRow infoRow = sheet.getRow(rowIndex + 1);
for (int colIndex = 0; colIndex < infos[rowIndex].length; colIndex++) {
HSSFCell cell = infoRow.getCell(colIndex);
if (cell != null) {
System.out.println(cell.getStringCellValue());
}
}
}
} catch (Exception e) {
System.out.println(e.getMessage());
e.printStackTrace();
}
}
}
3.程序运行结果
3.1 控制台

3.2Excel内容

抱歉!评论已关闭.