如题
要求:能够在分页页面上面直接点击一下按钮就可以直接导出。(不能导出全部的不要,java的要求能重用,js的最好不过希望操作方便)下面帮我看下这个怎么写成能重用的,下面这个泛形是没次换个类就要改类型的,希望能得到个不要重新写类型的import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.List;import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
/*程序需要设置
* 1.sheetName就是sheet的名字。
* 2.excelPath就是excel放置的地址与文件名字。
* 3.必须修改toExcel()方法中的List泛型的类型。
*
* 参数
* 1.list要写入excel的类的集合 其中泛型类User要依照情况变化
* 2.titles要写的各列的标题
*/public class ToExcel {
private String sheetName ;
private String excelPath;
public void toExcel(List<User> list, String[] titles) { HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet(this.getSheetName());
int nowRow = 0;
if (titles.length > 0) {
HSSFRow row = sheet.createRow(nowRow);
for (int i = 0; i < titles.length; i++) {
HSSFCell cell = row.createCell((short) i);
cell.setCellValue(titles[i]);
}
nowRow++;
}
if (list.size() <= 0) {
System.out.println("==========================");
System.out.println("没有数据!!!!!");
System.out.println("==========================");
return;
}
//反射出getter方法
Class c = list.get(0).getClass();
Field[] f = c.getDeclaredFields();
Method[] m =new Method[f.length];
for (int i = 0; i < f.length; i++) {
String methodName = "get"
+ f[i].getName().substring(0, 1).toUpperCase()
+ f[i].getName().substring(1);
try {
m[i] = c.getDeclaredMethod(methodName, null);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//反射出getter方法
for (int i = 0; i < list.size(); i++) { HSSFRow row = sheet.createRow(nowRow);
for (int j = 0; j < f.length; j++) {
HSSFCell cell = row.createCell((short) j);
try {
Object o = m[j].invoke(list.get(i), null);
cell.setCellValue(o.toString());
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
nowRow++;
}
try {
FileOutputStream out = new FileOutputStream(this.getExcelPath());
wb.write(out);
out.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public String getSheetName() {
return sheetName;
}
public void setSheetName(String sheetName) {
this.sheetName = sheetName;
}
public String getExcelPath() {
return excelPath;
}
public void setExcelPath(String excelPath) {
this.excelPath = excelPath;
}
}
要求:能够在分页页面上面直接点击一下按钮就可以直接导出。(不能导出全部的不要,java的要求能重用,js的最好不过希望操作方便)下面帮我看下这个怎么写成能重用的,下面这个泛形是没次换个类就要改类型的,希望能得到个不要重新写类型的import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.List;import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
/*程序需要设置
* 1.sheetName就是sheet的名字。
* 2.excelPath就是excel放置的地址与文件名字。
* 3.必须修改toExcel()方法中的List泛型的类型。
*
* 参数
* 1.list要写入excel的类的集合 其中泛型类User要依照情况变化
* 2.titles要写的各列的标题
*/public class ToExcel {
private String sheetName ;
private String excelPath;
public void toExcel(List<User> list, String[] titles) { HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet(this.getSheetName());
int nowRow = 0;
if (titles.length > 0) {
HSSFRow row = sheet.createRow(nowRow);
for (int i = 0; i < titles.length; i++) {
HSSFCell cell = row.createCell((short) i);
cell.setCellValue(titles[i]);
}
nowRow++;
}
if (list.size() <= 0) {
System.out.println("==========================");
System.out.println("没有数据!!!!!");
System.out.println("==========================");
return;
}
//反射出getter方法
Class c = list.get(0).getClass();
Field[] f = c.getDeclaredFields();
Method[] m =new Method[f.length];
for (int i = 0; i < f.length; i++) {
String methodName = "get"
+ f[i].getName().substring(0, 1).toUpperCase()
+ f[i].getName().substring(1);
try {
m[i] = c.getDeclaredMethod(methodName, null);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//反射出getter方法
for (int i = 0; i < list.size(); i++) { HSSFRow row = sheet.createRow(nowRow);
for (int j = 0; j < f.length; j++) {
HSSFCell cell = row.createCell((short) j);
try {
Object o = m[j].invoke(list.get(i), null);
cell.setCellValue(o.toString());
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
nowRow++;
}
try {
FileOutputStream out = new FileOutputStream(this.getExcelPath());
wb.write(out);
out.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public String getSheetName() {
return sheetName;
}
public void setSheetName(String sheetName) {
this.sheetName = sheetName;
}
public String getExcelPath() {
return excelPath;
}
public void setExcelPath(String excelPath) {
this.excelPath = excelPath;
}
}
我现在就问能不能搞个重用的 包装起来!
以后直接当做工具包用
我希望能做个复用高的