jxls引入excel模版导出excel报表报错 jar包有点乱啊。。用 jxls,但是为什么错误信息里还有POIjar包得信息??你把运行期的jar包,整理下把。用Tomcat的话就是WEB-INF/lib下的jar包。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 XLSTransformer 类是应用 jxls的import net.sf.jxls.transformer.XLSTransformer;Workbook 是应用poi的import org.apache.poi.ss.usermodel.Workbook;workbook 引用jxl类型转换不了,上网看其他例子也是用poi的 提示java.lang.NoSuchMethodError: org.apache.poi.hssf.usermodel.HSSFSheet.removeRow发的代码里怎么没有用到removeRow的地方呢 我觉得他代码编译过了,但是运行时有问题。1)他运行和实行用的jar不一样。2)他用的jar里再用别的jar,那个别的jar有点老。 buildpath 好几次,jar包的版本也换了好几个,poi的jar包 从3.2换到3.8 依然这错误 package com.test;import java.io.File;import java.io.FileOutputStream;import java.util.ArrayList;import java.util.List;import jxl.Workbook;import jxl.format.Alignment;import jxl.format.Colour;import jxl.format.UnderlineStyle;import jxl.write.Label;import jxl.write.Number;import jxl.write.WritableCellFormat;import jxl.write.WritableFont;import jxl.write.WritableSheet;import jxl.write.WritableWorkbook;/** * 生成excel的工具类,不用模板 * @version 2.0 * */public class ExportExcel { private static ExportExcel bean = new ExportExcel(); public static ExportExcel getBean() { return bean; } private FileOutputStream output; private WritableWorkbook book; /** * 导出excel生成文件 * @param base 根目录(文件存放的目录) * @param fileName 文件名 * @param fullDatas 要导出的数据(这里必须存放String类型的数组) * @param title 文件的标题(第一行) * @param coteWidths 每一列的宽度 * @param titles 表头标题 * @param numCote 需要数字格式化的列的索引 * @return String 返回输出文件的物理路径 * */ @SuppressWarnings("unchecked") public String reportExcel(String base, String fileName, List fullDatas, String title, int[] coteWidths, String[] titles, int[] numCote) { try { createFolder(base);//如果文件夹不存在就创建 String filePath = base + fileName;//输出文件路径 WritableCellFormat cellFormat = new WritableCellFormat(); cellFormat.setAlignment(Alignment.CENTRE);//居中显示 WritableSheet sheet = getExcel(filePath, title, coteWidths, titles);//获得工作簿 if(null != sheet){ for(int i = 0; i < fullDatas.size(); i++){ String[] fullData = (String[]) fullDatas.get(i); if(fullData.length == coteWidths.length){//确保数据个数一致 for (int j = 0; j < fullData.length; j++) { boolean isNumeric = false;//判断该列是否需要数字化格式 if(null != numCote && numCote.length > 0){ for(int c = 0; c < numCote.length; c++){ if(j == numCote[c]){ isNumeric = true; break; } } } if(isNumeric){ Double numeric = Double.parseDouble(fullData[j]); Number number = new Number(j, i + 2, numeric, cellFormat); sheet.addCell(number);//数字格式化 }else { Label label = new Label(j, i + 2, fullData[j], cellFormat); sheet.addCell(label);//文本数据 } } } } } book.write(); book.close(); output.close(); return filePath; } catch (Exception e) { e.printStackTrace(); } return ""; } // 生产excel工作簿 private WritableSheet getExcel(String filePath, String title, int coteWidths[], String[] titles) { try { if(coteWidths.length != titles.length){ return null; } output = new FileOutputStream(filePath);//文件输出流 book = Workbook.createWorkbook(output); // 创建文件 WritableSheet sheet = book.createSheet("sheet", 0); // 创建工作薄 //标题样式 WritableFont headFont = new WritableFont( WritableFont.ARIAL, 15, WritableFont.BOLD, false, UnderlineStyle.NO_UNDERLINE, Colour.BLACK); WritableCellFormat headCellFormat = new WritableCellFormat(headFont); headCellFormat.setAlignment(Alignment.CENTRE);//居中显示 //表头样式 WritableFont titleFont = new WritableFont( WritableFont.ARIAL, 11, WritableFont.BOLD, false, UnderlineStyle.NO_UNDERLINE, Colour.BLACK); WritableCellFormat titleCellFormat = new WritableCellFormat(titleFont); titleCellFormat.setAlignment(Alignment.CENTRE); // 设置标题 sheet.mergeCells(0, 0, titles.length - 1, 0);//合并单元格 Label label = new Label(0, 0, title ,headCellFormat); sheet.addCell(label);//标题 // 设置表头 for (int i = 0; i < coteWidths.length; i++) { sheet.setColumnView(i, coteWidths[i]); // 设置列的宽度 Label label1 = new Label(i, 1, titles[i], titleCellFormat); sheet.addCell(label1);//表头标题 } return sheet; } catch (Exception e) { e.printStackTrace(); } return null; } // 创建文件夹 public void createFolder(String folderPath) { try { File folder = new File(folderPath); if (!folder.exists()) { folder.mkdirs(); } } catch (Exception e) { e.printStackTrace(); } } @SuppressWarnings("unchecked") public static void main(String[] args) { String base = "D:\\opt\\log\\", fileName = "student.xls", title= "学生成绩表"; String[] titles = new String[]{ "学号", "姓名", "语文", "数学", "排名" }; int[] coteWidths = new int[]{ 20, 30, 20, 20, 20 }; int[] numCote = new int[]{ 0, 2, 3, 4 }; List list = new ArrayList(); for (int i = 1; i <= numCote.length; i++) { list.add(new String[]{ "201000" + i, i + "号学生", String.valueOf(100 - i), String.valueOf(95 - i), String.valueOf(i) }); } System.out.println(ExportExcel.getBean().reportExcel(base, fileName, list, title, coteWidths, titles, numCote)); }} 多谢,但对应某些特定模版的excel报表,jxl的标签还是会比较方便点。不用数格子,画格子。我先试下这段代码 楼主你的buildpath里的jar文件如果有类似poi-X.X-日期.jar的话下面的地址,下个最新版,换了试试看。http://www.apache.org/dyn/closer.cgi/poi/release/bin/poi-bin-3.8-20120326.zip 当然如果没有的话,加上放到buildpath最上面试试看。 有的,项目里的poi包是 poi-3.8-20120326.jar 楼主赶紧换商业报表吧,开源报表一遇到问题就搞死人,建议你试试商业报表finereport 请高手帮忙看看怎么解决 hibernate 查询结果为null oracle JSF Bean之间参数的传递 我这个错误该怎么更正 更改tomcat默认主页问题 试了N次 但我还是失败了 Servlet自动文件下载中文文件名乱码问题,怎样解决 请推荐一个免费的支持J2EE服务器,谢谢! 我想把数据导出的excel,怎么样在程序里实现合并excel的单元格?? Jubilder8的问题 如何在以台电脑上同时向Internet发布ASP,JSP的网页??(在线等) 初学struts2的bbs程序完成了,但多点几下就死机 linux下路径为空
Workbook 是应用poi的import org.apache.poi.ss.usermodel.Workbook;workbook 引用jxl类型转换不了,上网看其他例子也是用poi的
我觉得他代码编译过了,但是运行时有问题。
1)他运行和实行用的jar不一样。
2)他用的jar里再用别的jar,那个别的jar有点老。
buildpath 好几次,jar包的版本也换了好几个,poi的jar包 从3.2换到3.8 依然这错误
package com.test;import java.io.File;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.List;
import jxl.Workbook;
import jxl.format.Alignment;
import jxl.format.Colour;
import jxl.format.UnderlineStyle;
import jxl.write.Label;
import jxl.write.Number;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;/**
* 生成excel的工具类,不用模板
* @version 2.0
* */
public class ExportExcel {
private static ExportExcel bean = new ExportExcel();
public static ExportExcel getBean() {
return bean;
}
private FileOutputStream output;
private WritableWorkbook book;
/**
* 导出excel生成文件
* @param base 根目录(文件存放的目录)
* @param fileName 文件名
* @param fullDatas 要导出的数据(这里必须存放String类型的数组)
* @param title 文件的标题(第一行)
* @param coteWidths 每一列的宽度
* @param titles 表头标题
* @param numCote 需要数字格式化的列的索引
* @return String 返回输出文件的物理路径
* */
@SuppressWarnings("unchecked")
public String reportExcel(String base, String fileName, List fullDatas,
String title, int[] coteWidths, String[] titles, int[] numCote) {
try {
createFolder(base);//如果文件夹不存在就创建
String filePath = base + fileName;//输出文件路径
WritableCellFormat cellFormat = new WritableCellFormat();
cellFormat.setAlignment(Alignment.CENTRE);//居中显示
WritableSheet sheet = getExcel(filePath, title, coteWidths, titles);//获得工作簿
if(null != sheet){
for(int i = 0; i < fullDatas.size(); i++){
String[] fullData = (String[]) fullDatas.get(i);
if(fullData.length == coteWidths.length){//确保数据个数一致
for (int j = 0; j < fullData.length; j++) {
boolean isNumeric = false;//判断该列是否需要数字化格式
if(null != numCote && numCote.length > 0){
for(int c = 0; c < numCote.length; c++){
if(j == numCote[c]){
isNumeric = true;
break;
}
}
}
if(isNumeric){
Double numeric = Double.parseDouble(fullData[j]);
Number number = new Number(j, i + 2, numeric, cellFormat);
sheet.addCell(number);//数字格式化
}else {
Label label = new Label(j, i + 2, fullData[j], cellFormat);
sheet.addCell(label);//文本数据
}
}
}
}
}
book.write();
book.close();
output.close();
return filePath;
} catch (Exception e) {
e.printStackTrace();
}
return "";
} // 生产excel工作簿
private WritableSheet getExcel(String filePath, String title, int coteWidths[], String[] titles) {
try {
if(coteWidths.length != titles.length){
return null;
}
output = new FileOutputStream(filePath);//文件输出流
book = Workbook.createWorkbook(output); // 创建文件
WritableSheet sheet = book.createSheet("sheet", 0); // 创建工作薄
//标题样式
WritableFont headFont = new WritableFont(
WritableFont.ARIAL, 15, WritableFont.BOLD, false,
UnderlineStyle.NO_UNDERLINE, Colour.BLACK);
WritableCellFormat headCellFormat = new WritableCellFormat(headFont);
headCellFormat.setAlignment(Alignment.CENTRE);//居中显示
//表头样式
WritableFont titleFont = new WritableFont(
WritableFont.ARIAL, 11, WritableFont.BOLD, false,
UnderlineStyle.NO_UNDERLINE, Colour.BLACK);
WritableCellFormat titleCellFormat = new WritableCellFormat(titleFont);
titleCellFormat.setAlignment(Alignment.CENTRE);
// 设置标题
sheet.mergeCells(0, 0, titles.length - 1, 0);//合并单元格
Label label = new Label(0, 0, title ,headCellFormat);
sheet.addCell(label);//标题
// 设置表头
for (int i = 0; i < coteWidths.length; i++) {
sheet.setColumnView(i, coteWidths[i]); // 设置列的宽度
Label label1 = new Label(i, 1, titles[i], titleCellFormat);
sheet.addCell(label1);//表头标题
}
return sheet;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
// 创建文件夹
public void createFolder(String folderPath) {
try {
File folder = new File(folderPath);
if (!folder.exists()) {
folder.mkdirs();
}
} catch (Exception e) {
e.printStackTrace();
}
}
@SuppressWarnings("unchecked")
public static void main(String[] args) {
String base = "D:\\opt\\log\\", fileName = "student.xls", title= "学生成绩表";
String[] titles = new String[]{ "学号", "姓名", "语文", "数学", "排名" };
int[] coteWidths = new int[]{ 20, 30, 20, 20, 20 };
int[] numCote = new int[]{ 0, 2, 3, 4 };
List list = new ArrayList();
for (int i = 1; i <= numCote.length; i++) {
list.add(new String[]{ "201000" + i, i + "号学生", String.valueOf(100 - i), String.valueOf(95 - i), String.valueOf(i) });
}
System.out.println(ExportExcel.getBean().reportExcel(base, fileName, list, title, coteWidths, titles, numCote));
}}
poi-X.X-日期.jar
的话下面的地址,下个最新版,换了试试看。
http://www.apache.org/dyn/closer.cgi/poi/release/bin/poi-bin-3.8-20120326.zip
有的,项目里的poi包是 poi-3.8-20120326.jar