一环境配置
1.安装j2se1.4.1(也可以是其他版本,最好是j2se1.3.1以上的版本)
2.设定JAVA_HOME和CLASSPATH
3.取得POI项目的文件包,可以从下面地址下载
http://jakarta.apache.org/builds/jakarta-poi/release/bin/jakarta-poi-1.5.1-final-bin.zip
4.使用压缩软件解开这个文件,将其Build目录下的jakarta-poi-1.5.1-final-20020615.jar设到CLASSPATH中。
现在我们就可以开始写我们的Excel文件了。
我们先写一个空的Excel文件
源码如下
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import java.io.IOException;
import java.io.FileOutputStream;
public class NewSheet
{
public static void main(String[] args)
throws IOException
{
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet1 = wb.createSheet("new sheet");
HSSFSheet sheet2 = wb.createSheet("second sheet");
FileOutputStream fileOut = new FileOutputStream("workbook.xls");
wb.write(fileOut);
fileOut.close();
}
}
(注:这是POI项目中的一个例子,可以在POI的源码包org.apache.poi.hssf.usermodel.examples中找到)
编译运行,
javax NewSheet.java
java NewSheet
你可以在当前目录下找到workbook.xls文件,用Excel打开,一样吧!^_^
下面我们改写这个例子,先建立一个Coding.xls文件作为模版,在其中的某一行写入#ROWB
隔开几个单元格写入#COLE(我的源码中指定行列数都小于50)
下面是源码
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.poifs.filesystem.*;
import org.apache.poi.hssf.util.*;
import java.io.*;
public class NewSheet
{
public static void main(String[] args)
throws IOException
{
POIFSFileSystem fs =
new POIFSFileSystem(new FileInputStream("Coding.xls"));
HSSFWorkbook wb = new HSSFWorkbook(fs);
HSSFSheet sheet = wb.getSheetAt(0);
HSSFCellStyle style = wb.createCellStyle();
//style.setFillBackgroundColor(HSSFCellStyle.RED);
style.setFillForegroundColor(HSSFCellStyle.RED);
style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
String location = new String("");
short rowNum = 0;
short cellNum = 0;
int columns = 0;
int rows = 0;
HSSFRow row = sheet.getRow(rowNum);
HSSFCell cell = row.getCell((short)cellNum);
location = cell.getStringCellValue();
while(!location.equals("#ROWB")){
System.out.println("LIN"+rowNum+" "+location);
rowNum ++;
row = sheet.getRow(rowNum);
cell = row.getCell((short)cellNum);
location = cell.getStringCellValue();
if(rowNum > 50) break;
}
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setCellStyle(style);
cell.setCellValue("Replace ROWB OK");
//cell = row.createCell((short) 3);
while(!location.equals("#COLE")){
System.out.println("COL"+cellNum+" "+location);
if(cellNum != 0){
cell.setCellValue("ADD OK");
}
cellNum ++;
cell = row.getCell((short)cellNum);
location = cell.getStringCellValue();
if(cellNum > 50) break;
}
columns = cellNum;
HSSFCellStyle style1 = wb.createCellStyle();
style1.setFillForegroundColor(HSSFCellStyle.BLUE);
style1.setFillPattern(HSSFCellStyle.SQUARES);
cell.setCellStyle(style1);
cell.setCellValue("Replace COLE OK");
// Write the output to a file
FileOutputStream fileOut = new FileOutputStream("workbook1.xls");
wb.write(fileOut);
fileOut.close();
}
}
编译运行,
javax NewSheet.java
java NewSheet
现在我们可以从模版读取,并在指定位置写入我们的数据了。
其他的API的使用请参考Javadoc文档,以及http://jakarta.apache.org/poi/
本文介绍如何使用Apache POI库在已存在的Excel模板中读取并动态填充数据,通过示例代码展示从模板读取内容、定位特定单元格并进行替换的操作,最终生成新的Excel文件。
1945

被折叠的 条评论
为什么被折叠?



