概述
这是我自己写的创建Excel的一个类,仅供参考,用的是jxl.jar(见附件)。偷懒总是有方法的,感觉像创建Ext的Grid一样。
接口定义:
public void generateExcel(List<?> entities, Column[] columns, String fileName);
实现:
package com.yingxia.trms.excel;
import java.io.File;
import java.lang.reflect.Method;
import java.util.List;
import jxl.Workbook;
import jxl.format.Colour;
import jxl.write.Label;
import jxl.write.WritableCell;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
public class ExcelGeneratorImpl implements ExcelGenerator {
@Override
public void generateExcel(List<?> entities, Column[] columns, String fileName) {
try {
String path = ExcelGeneratorImpl.class.getResource("/").getPath();
path = path.replace("WEB-INF/classes/", "") + "excel/" + fileName + ".xls";
File file = new File(path);
if(file.exists()) {
file.delete();
}
WritableWorkbook workbook = Workbook.createWorkbook(new File(path));
WritableSheet sheet = workbook.createSheet("First sheet", 0);
// header
for (int col = 0; col < columns.length; col++) {
Label labelHeader = new Label(col, 0, columns[col].getHeader());
sheet.addCell(labelHeader);
}
// body
for (int row = 1; row <= entities.size(); row++) {
for (int col = 0; col < columns.length; col++) {
String columnName = columns[col].getName();
Object columnValue = getFieldValue(entities.get(row - 1), columnName);
WritableCell cell = null;
if(columns[col].getType().equals("string")) {
cell = new Label(col, row, columnValue.toString());
} else {
cell = new jxl.write.Number(col, row, Double.valueOf(columnValue.toString()));
}
sheet.addCell(cell);
}
}
// sum
for (int col = 0; col < columns.length; col++) {
Column column =
columns[col];
if(column.isNeedSum() && column.getType().equals("number")) {
Double columnSum = 0.0;
for (int row = 1; row <= entities.size(); row++) {
Object fieldValue = getFieldValue(entities.get(row - 1), column.getName());
columnSum += Double.valueOf(fieldValue.toString());
}
WritableFont font = new WritableFont(WritableFont.ARIAL);
font.setColour(Colour.RED);
font.setBoldStyle(WritableFont.BOLD);
jxl.write.Number number = new jxl.write.Number(col, entities.size() + 1, columnSum, new WritableCellFormat(font));
sheet.addCell(number);
}
}
workbook.write();
workbook.close();
} catch(Exception ex) {
ex.printStackTrace();
}
}
private Object getFieldValue(Object obj, String fieldName) throws Exception {
String getMethodName = "get" + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1);
Method getMethod = obj.getClass().getMethod(getMethodName);
return getMethod.invoke(obj);
}
}
这其中用到了一个自定义的Column类
package com.yingxia.trms.excel;
public class Column {
private String name;
private String header;
private String type; // string, number
private boolean needSum = false; // must be number
public Column() {
}
public Column(String name, String header, String type) {
super();
this.name = name;
this.header = header;
this.type = type;
}
public Column(String name, String header, String type, boolean needSum) {
super();
this.name = name;
this.header = header;
this.type = type;
this.needSum = needSum;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getHeader() {
return header;
}
public void setHeader(String header) {
this.header = header;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public boolean isNeedSum() {
return needSum;
}
public void setNeedSum(boolean needSum) {
this.needSum = needSum;
}
}
最后
以上就是活泼雪糕为你收集整理的用java操作excel,jexcelapi的全部内容,希望文章能够帮你解决用java操作excel,jexcelapi所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复