jxlがexcelファイルをエクスポートする例はjava反射で
最近javaの反射内容を見て、自分は少し概念を持っています.主にMethodとFieldオブジェクトの基本的な使い方を学びました.だから反射でエクスポートしました.主な役割はlistリストのオブジェクトの属性をエクスポートする属性に対応させることであり,ビジネス層ではオブジェクトセットを入れるだけである.一定の単純なルールで対応するexcelファイルを生成できます.よく考えられない点をご指摘ください
変換クラス:
ルール・クラス:
特定のルール・クラス
voオブジェクトクラス
テストクラス:
次は工事全体です.ideaで作ったのはeclipseと衝突する可能性があります.
変換クラス:
public class CommunUtil {
// Map
public static Map getMap(Object obj) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException, InstantiationException {
Map objectMsp = new HashMap();
//
Class classType = obj.getClass();
//
Object objCopy = classType.getConstructor(new Class[]{}).newInstance(new Object[]{});
Field[] fields = classType.getDeclaredFields();
for(int i=0;i<fields.length;i++){
Field field = fields[i];
String fieldName = field.getName();//
//getXXX setXXX
String getMethodName = "get" + fieldName.substring(0,1).toUpperCase() + fieldName.substring(1);
Method getMethod = classType.getDeclaredMethod(getMethodName,new Class[]{});
Object value = getMethod.invoke(obj,new Object[]{});
objectMsp.put(fieldName,value);
//
}
return objectMsp;
}
//
public static Object getObject(Object obj) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException, InstantiationException {
Map objectMsp = new HashMap();
//
Class classType = obj.getClass();
//
Object objCopy = classType.getConstructor(new Class[]{}).newInstance(new Object[]{});
Field[] fields = classType.getDeclaredFields();
for(int i=0;i<fields.length;i++){
Field field = fields[i];
String fieldName = field.getName();//
//getXXX setXXX
String getMethodName = "get" + fieldName.substring(0,1).toUpperCase() + fieldName.substring(1);
String setMethodName = "set" + fieldName.substring(0,1).toUpperCase() + fieldName.substring(1);
Method getMethod = classType.getDeclaredMethod(getMethodName,new Class[]{});
Method setMethod = classType.getDeclaredMethod(setMethodName,new Class[]{field.getType()});
Object value = getMethod.invoke(obj,new Object[]{});
// System.out.println("fieldName : " + value + field.getType());
//
setMethod.invoke(objCopy,new Object[]{value});
}
return objCopy;
}
}
ルール・クラス:
public abstract class Rules implements Serializable {
// StreamTokenizer streamTokenizer =
protected List[] rowTitle;
protected Map<String,String> rowMap;
protected List cols;
protected Map<Integer,String> rMap;
public Map<Integer, String> getRMap() {
return rMap;
}
public void setRMap(Map<Integer, String> rMap) {
this.rMap = rMap;
}
public Rules(){
rMap = new HashMap<Integer,String>();
rowMap = new HashMap<String,String>();
cols = new ArrayList();
this.init();
}
public Rules(Map<String,String> r){
rMap = new HashMap<Integer,String>();
this.rowMap = r;
cols = new ArrayList();
this.init();
}
public abstract void init();
public List[] getRowTitle() {
return rowTitle;
}
public void setRowTitle(List[] rowTitle) {
this.rowTitle = rowTitle;
}
public abstract void setRowTitle();
public Map<String, String> getRowMap() {
return rowMap;
}
public void setRowMap(Map<String, String> rowMap) {
this.rowMap = rowMap;
}
public List getCols() {
return cols;
}
public void setCols(List cols) {
this.cols = cols;
}
}
特定のルール・クラス
public class UserRules extends Rules {
public void init() {
//To change body of implemented methods use File | Settings | File Templates.
//
this.rMap.put(new Integer(rMap.size()),"userName");
this.rMap.put(new Integer(rMap.size()),"userAddress");
this.rMap.put(new Integer(rMap.size()),"userAge");
this.rMap.put(new Integer(rMap.size()),"clickCount");
this.rMap.put(new Integer(rMap.size()),"userLong");
this.rMap.put(new Integer(rMap.size()),"userSalary");
// this.rMap.put(new Integer(rMap.size()),"userMount");
this.setRowTitle();
}
//
public void setRowTitle() {
//To change body of implemented methods use File | Settings | File Templates.
this.rowTitle = new List[2];
List rList = new ArrayList();
rList.add(new String[]{" ","1","0"});
rList.add(new String[]{" ","0","1"});
rList.add(new String[]{" ","0","1"});
rList.add(new String[]{" ","1","1"});
this.rowTitle[0] = rList;
List rList2 = new ArrayList();
rList2.add(new String[]{" ","0","0"});
rList2.add(new String[]{" ","0","0"});
rList2.add(new String[]{" ","0","0"});
rList2.add(new String[]{" ","0","0"});
rList2.add(new String[]{" ","0","0"});
// rList2.add(new String[]{" 1","0","0"});
// rList2.add(new String[]{" 2","0","0"});
// rList.add(new String[]{" ","0","0"});
this.rowTitle[1] = rList2;
}
}
voオブジェクトクラス
public class User {
private String userName;
private String userAddress;
private int userAge;
private int clickCount;
private long userLong;
private String userSalary;
private String userMount;
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getUserAddress() {
return userAddress;
}
public void setUserAddress(String userAddress) {
this.userAddress = userAddress;
}
public int getUserAge() {
return userAge;
}
public void setUserAge(int userAge) {
this.userAge = userAge;
}
public int getClickCount() {
return clickCount;
}
public void setClickCount(int clickCount) {
this.clickCount = clickCount;
}
public long getUserLong() {
return userLong;
}
public void setUserLong(long userLong) {
this.userLong = userLong;
}
public String getUserSalary() {
return userSalary;
}
public void setUserSalary(String userSalary) {
this.userSalary = userSalary;
}
public String getUserMount() {
return userMount;
}
public void setUserMount(String userMount) {
this.userMount = userMount;
}
}
テストクラス:
public class ExcelWriter {
public static void main(String [] args){
// Date d1 = new Date(System.currentTimeMillis()-604800000L);
// Date d2 = new Date();
// System.out.println(d1+"
" + d2);
ExcelWriter writer = new ExcelWriter();
List userList = new ArrayList();
for(int i=0;i<10;i++){
User user = new User();
user.setClickCount(1+i);
user.setUserAddress("userAddress" + i);
user.setUserAge(20+i);
user.setUserLong(new Long(10000)+i);
user.setUserMount("userMount" + i);
user.setUserName("userName" + i);
user.setUserSalary("userSalary" + i);
userList.add(user);
}
try {
writer.exportExcel(userList); //
UserRules ur = new UserRules();
writer.exportExcel(userList,ur);
} catch (InvocationTargetException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
} catch (NoSuchMethodException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
} catch (InstantiationException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
} catch (IllegalAccessException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
} catch (IOException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
} catch (WriteException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
}
//
public void exportExcel(List list,Rules rules) throws IOException, WriteException, InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException {
List[] rowList = rules.getRowTitle();
Label lable = null;
File file = new File("e:\\mytest\\testjxl3.xls");
WritableWorkbook workbook = Workbook.createWorkbook(file);
WritableSheet sheet = workbook.createSheet("TestCreateExcel", 0);
Map rMap = rules.getRMap(); //
//
int i=0; //
for(;i<rowList.length;i++){
List row = rowList[i];
for(int cop=0, j=0;j<row.size();cop++,j++){ //
String [] title = (String[]) row.get(j);
String ctitle = title[0];
lable = new Label(cop,i,ctitle);
sheet.addCell(lable);
int r = Integer.parseInt(title[1]);
int c = Integer.parseInt(title[2]) ;
if( r != 0 || c != 0){ //
sheet.mergeCells(cop,i,cop+c,r+i);
cop += c;
}
}
}
//
List cols = rowList[rowList.length-1]; //
int rowCount = list.size() + rowList.length; //
if( list != null && list.size()>0){
for(int n=0;n<list.size();n++){
Map objectMap = CommunUtil.getMap( list.get(n)); // == Map
for(int k=0;k<rMap.size();k++){
Object key = rMap.get(new Integer(k));
String columnValue = (objectMap.get(key)+"").trim();
lable = new Label(k,n+rowList.length,columnValue);
sheet.addCell(lable);
}
}
}
workbook.write();
workbook.close();
}
public void exportExcel(List list) throws InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException {
File file = new File("e:\\mytest\\testjxl2.xls");
try {
System.out.println("======= ============");
WritableWorkbook workbook = Workbook.createWorkbook(file);
WritableSheet sheet = workbook.createSheet("TestCreateExcel", 0);
// , excel
Label l = null;
jxl.write.Number n = null;
jxl.write.DateTime d = null;
// , Excel
WritableFont headerFont = new WritableFont(WritableFont.ARIAL, 12, WritableFont.BOLD,
false, UnderlineStyle.NO_UNDERLINE, jxl.format.Colour.BLUE);
WritableCellFormat headerFormat = new WritableCellFormat(headerFont);
WritableFont titleFont = new WritableFont(WritableFont.ARIAL, 10, WritableFont.NO_BOLD,
false, UnderlineStyle.NO_UNDERLINE, jxl.format.Colour.RED);
WritableCellFormat titleFormat = new WritableCellFormat(titleFont);
WritableFont detFont = new WritableFont(WritableFont.ARIAL, 10, WritableFont.NO_BOLD,
false, UnderlineStyle.NO_UNDERLINE, jxl.format.Colour.BLACK);
WritableCellFormat detFormat = new WritableCellFormat(detFont);
NumberFormat nf = new NumberFormat("0.00000"); // Number
WritableCellFormat priceFormat = new WritableCellFormat(detFont, nf);
DateFormat df = new DateFormat("yyyy-MM-dd");//
WritableCellFormat dateFormat = new WritableCellFormat(detFont, df);
//
l = new Label(0, 0, " ", headerFormat);
sheet.addCell(l);
sheet.mergeCells(0, 0, 0, 1); // 0 3 0 0 4 c,r,c,r
l = new Label(1,0," ", headerFormat);
sheet.addCell(l);
sheet.mergeCells(1, 0, 2, 0);
l = new Label(3,0,"ADSL ", headerFormat);
sheet.addCell(l);
sheet.mergeCells(3, 0, 4, 0);
l = new Label(5,0," ", headerFormat);
sheet.addCell(l);
sheet.mergeCells(5, 0, 6, 0);
//
l = new Label(1, 1, " ", headerFormat);
sheet.addCell(l);
l = new Label(2, 1, " ", headerFormat);
sheet.addCell(l);
l = new Label(3, 1, " ", headerFormat);
sheet.addCell(l);
l = new Label(4, 1, " ", headerFormat);
sheet.addCell(l);
l = new Label(5, 1, " ", headerFormat);
sheet.addCell(l);
l = new Label(6, 1, " ", headerFormat);
sheet.addCell(l);
if(list != null && list.size()>0){
for(int i=0;i<list.size();i++){
Map objectMap = CommunUtil.getMap( list.get(i)); // == Map
int j=0;//
for(Object o : objectMap.keySet()){
//String key = (String) o;
String pvalue = objectMap.get(o)+"".trim();
//System.out.println("pvalue== " + pvalue);
//System.out.println("userValue"+i+" "+o+" " + objectMap.get(o));
Label lb = new Label(j,i+2,pvalue);
sheet.addCell(lb);
j++;
}
}
}
workbook.write();
workbook.close();
System.out.println("==================== =====================");
} catch (IOException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
} catch (RowsExceededException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
} catch (WriteException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
}
}
次は工事全体です.ideaで作ったのはeclipseと衝突する可能性があります.