怎么把从数据库取出来的数据生成一个Excel文件,并显示数据
解决方案 »
- 求助 jquery
- 关于getHibernateTemplate().find()查询问题
- 使用xfire插件生成客户端代码,这种功能支持jdk1.4吗?应该注意什么?
- 求问两个JAVA applet 问题
- hibernate mysql setFirstResult 方法问题...
- 跪求高手帮看这个错误啊
- 有兴趣的朋友过来看看,一个不用插入数据库就可以统计结果的方法?解决就把我所有的分都给.
- <%=rs.getInt("zjid") %>,=怎么成了错误的开始符了呢
- Java基础:Java中怎么处理异常
- 请问一下:哪里有jsp的免费书下载
- 用jxl导入到Excel中,怎样给文字添加 删除线
- Spring注解注入的问题,看能解决吗?
推荐extremecomponents 组件
http://blog.csdn.net/kings988/archive/2009/12/29/5099589.aspx
jxl 数据量过大 会内存泄漏
建议用jacob1.9的 具体怎么配置 网上找下吧private ActiveXComponent xl;
private Dispatch workbooks = null;
private Dispatch workbook = null;
private Dispatch sheet = null;
private String filename = null;
private boolean readonly = false;
private Dispatch sheets = null;// 获得sheets集合对象 public void clean() { xl = null;
workbooks = null;
workbook = null;
sheet = null;
filename = null;
sheets = null;// 获得sheets集合对象
} // 打开Excel文档
public void OpenExcel(String file, boolean f) {
ComThread.InitSTA();
//ComThread.InitSTA(true);
try {
filename = file;
xl = new ActiveXComponent("Excel.Application");
xl.setProperty("Visible", new Variant(f));
workbooks = xl.getProperty("Workbooks").toDispatch();
workbook = Dispatch.invoke(
workbooks,
"Open",
Dispatch.Method,
new Object[] { filename, new Variant(false),
new Variant(readonly) },// 是否以只读方式打开
new int[1]).toDispatch();
} catch (Exception e) {
e.printStackTrace();
}
} // 关闭Excel文档
public void CloseExcel(boolean f) {
try {
Dispatch.call(workbook, "Save");
Dispatch.call(workbook, "Close", new Variant(f));
} catch (Exception e) {
e.printStackTrace();
} finally {
xl.invoke("Quit", new Variant[] {});
ComThread.Release();
// ComThread.InitMTA()
}
} // 写入值
public void SetValue(String position, String type, String value) {
sheet = Dispatch.get(workbook, "ActiveSheet").toDispatch();
Dispatch cell = Dispatch.invoke(sheet, "Range", Dispatch.Get,
new Object[] { position }, new int[1]).toDispatch();
Dispatch.put(cell, type, value);
cell = null;
} // 读取值
public String GetValue(String position) {
Dispatch cell = Dispatch.invoke(sheet, "Range", Dispatch.Get,
new Object[] { position }, new int[1]).toDispatch();
String value = Dispatch.get(cell, "Value").toString(); return value;
} // 添加新的工作表(sheet),(添加后为默认为当前激活的工作表)
public void addSheet() {
Dispatch.get(Dispatch.get(workbook, "sheets").toDispatch(), "add");
} // 修改当前工作表的名字
public void modifySheetName(String newName) {
Dispatch.put(getCurrentSheet(), "name", newName);
} // 得到当前工作表的名字
public String getSheetName() {
return Dispatch.get(sheet, "name").toString();
} // 得到工作薄的名字
public String getWorkbookName() {
return Dispatch.get(workbook, "name").toString();
} // 得到sheets的集合对象
public Dispatch getSheets() {
sheets = Dispatch.get(workbook, "sheets").toDispatch();
return sheets;
} // 通过工作表名字得到工作表(未实现)
public Dispatch getSheetByName(String name) {
//
return sheet;
} // 通过工作表索引得到工作表(未实现)
public Dispatch getSheetByIndex(String name) {
//
return sheet;
} // 得到当前sheet
public Dispatch getCurrentSheet() {
sheet = Dispatch.get(workbook, "ActiveSheet").toDispatch();
return sheet;
} // 得到sheet的总数
public int getSheetCount() {
int count = Dispatch.get(getSheets(), "count").toInt();
return count;
}
try {
File file = new File("\\test\\test_2\\WebRoot\\test\\test.xls");//给文件命名和设置路径
WritableWorkbook book = Workbook.createWorkbook(file);
WritableSheet ws = book.createSheet("第一页", 0);//设置页
Label label = new Label(0,0,"我爱我家sssssss");//设置第一行里面的第一个单元格的内容
ws.addCell(label); //添加行
book.write();
book.close();//完成xls生成
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}以上是生成xls文件的代码