======= JSP 操作 Excel 的问题 =================== 我想用JSP来操作Excel。在网上找到了相关资料。也下载了 poi-src-2.5.1-final-20040804.zip。但是不知道怎样用。我的环境是 Tomcat 5 ,JDK1.5.0_05 ,Win2k AS 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 这是JAVA的例子,改成JSP不难吧:mport org.apache.poi.hssf.usermodel.*;import java.io.*;import java.util.*;import org.apache.poi.poifs.filesystem.POIFSFileSystem; public class exceltest { public static void writeexcel() throws IOException { HSSFWorkbook wb = new HSSFWorkbook();// 建立新HSSFWorkbook对象 HSSFSheet sheet = wb.createSheet("new sheet");// 建立新的sheet对象 0 开始 HSSFRow row = sheet.createRow((short)0);// 建立新行 HSSFCell cell = row.createCell((short)0);// 建立新cell cell.setCellValue(3.3);//设置cell的整数类型的值 row.createCell((short)1).setCellValue(1.2);// 设置cell浮点类型的值 row.createCell((short)2).setCellValue("test");// 设置cell字符类型的值 row.createCell((short)3).setCellValue(true);// 设置cell布尔类型的值 HSSFCellStyle cellStyle = wb.createCellStyle();// 建立新的cell样式 cellStyle.setDataFormat (HSSFDataFormat.getBuiltinFormat("m/d/yy h:mm"));// 设置cell样式为定制的日期格式 HSSFCell dCell =row.createCell((short)4); dCell.setCellValue(new Date());// 设置cell为日期类型的值 dCell.setCellStyle(cellStyle); // 设置该cell日期的显示格式 HSSFCell csCell =row.createCell((short)5); csCell.setEncoding(HSSFCell.ENCODING_UTF_16);// 设置cell编码解决中文高位字节截断 csCell.setCellValue("中文测试_Chinese Words Test");// 设置中西文结合字符串 row.createCell((short)6).setCellType (HSSFCell.CELL_TYPE_ERROR);// 建立错误cell FileOutputStream fileOut = new FileOutputStream("d:/workbook.xls"); wb.write(fileOut); fileOut.close(); System.out.println("hahaha"); } public static void readexcel() { POIFSFileSystem fs = null; HSSFWorkbook wb = null; try { fs = new POIFSFileSystem(new FileInputStream("d:/workbook.xls")); wb = new HSSFWorkbook(fs); } catch (IOException e) { e.printStackTrace(); } HSSFSheet sheet = wb.getSheetAt(0); HSSFRow row = sheet.getRow(0); HSSFCell cell = row.getCell((short)0); double msg = cell.getNumericCellValue(); System.out.println(msg); // String msg1 = cell.getCellFormula(); // System.out.println(msg1); } public static void main(String[] args) throws IOException { writeexcel(); System.out.println("Write OK"); readexcel(); System.out.println("Read OK"); } 我要实现的功能是:JSP做的网页列出数据库的数据.完后将指定的数据写到 指定的 Excel文件的指定的单元格里. JXL,我一直拿它来做Excel报表。设置格式,公式都比较方便。去sourceforge.net下载一个jxl吧,它自带的example看看就差不多了。 把上面的代码封装成一个JavaBean去实现就可以了。 关于导成jar过后的包里的文件的更改问题。 大家来挑挑错。说出合适理由的都有分。 think in java 的所有代码,做成chm文件了,很方便收藏的,大家下载做的收藏吧 讨论,关于使用Tab的问题 swing 窗口最小化后布局改变 package问题! 如何将jtable里面指定(不是selected,比如说第三行)的一行背景色改变. 关于System的setProperty/getProperty等问题(在线等待。。。。) 请教--有关java网络编程 图片上传BEAN ?2 请问"断言"怎么用啊?? 找错误!!!!谢谢
mport org.apache.poi.hssf.usermodel.*;
import java.io.*;
import java.util.*;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
public class exceltest {
public static void writeexcel() throws IOException {
HSSFWorkbook wb = new HSSFWorkbook();
// 建立新HSSFWorkbook对象
HSSFSheet sheet = wb.createSheet("new sheet");
// 建立新的sheet对象 0 开始
HSSFRow row = sheet.createRow((short)0);
// 建立新行
HSSFCell cell = row.createCell((short)0);
// 建立新cell
cell.setCellValue(3.3);//设置cell的整数类型的值
row.createCell((short)1).setCellValue(1.2);
// 设置cell浮点类型的值
row.createCell((short)2).setCellValue("test");
// 设置cell字符类型的值
row.createCell((short)3).setCellValue(true);
// 设置cell布尔类型的值
HSSFCellStyle cellStyle = wb.createCellStyle();
// 建立新的cell样式
cellStyle.setDataFormat
(HSSFDataFormat.getBuiltinFormat("m/d/yy h:mm"));
// 设置cell样式为定制的日期格式
HSSFCell dCell =row.createCell((short)4);
dCell.setCellValue(new Date());
// 设置cell为日期类型的值
dCell.setCellStyle(cellStyle);
// 设置该cell日期的显示格式
HSSFCell csCell =row.createCell((short)5);
csCell.setEncoding(HSSFCell.ENCODING_UTF_16);
// 设置cell编码解决中文高位字节截断
csCell.setCellValue("中文测试_Chinese Words Test");
// 设置中西文结合字符串
row.createCell((short)6).setCellType
(HSSFCell.CELL_TYPE_ERROR);
// 建立错误cell
FileOutputStream fileOut =
new FileOutputStream("d:/workbook.xls");
wb.write(fileOut);
fileOut.close();
System.out.println("hahaha");
}
public static void readexcel() {
POIFSFileSystem fs = null;
HSSFWorkbook wb = null;
try {
fs = new POIFSFileSystem(new FileInputStream("d:/workbook.xls"));
wb = new HSSFWorkbook(fs);
} catch (IOException e) {
e.printStackTrace();
}
HSSFSheet sheet = wb.getSheetAt(0);
HSSFRow row = sheet.getRow(0);
HSSFCell cell = row.getCell((short)0);
double msg = cell.getNumericCellValue();
System.out.println(msg);
// String msg1 = cell.getCellFormula();
// System.out.println(msg1);
}
public static void main(String[] args) throws IOException {
writeexcel();
System.out.println("Write OK");
readexcel();
System.out.println("Read OK");
}
设置格式,公式都比较方便。
去sourceforge.net下载一个jxl吧,它自带的example看看就差不多了。