java中的export⽅法实现导出excel⽂件
⽬录
2.导出列名
4.前端对接
5.前端代码
//导出⽂件接⼝
public String export(){
Export(exportList);
}
2.导出列名
private String myExport(List<BusinessDept> list){
com.ls.json.JSONObject info = new com.ls.json.JSONObject();
try{
List<String> headList = new ArrayList<String>(Arrays.asList("年", "⽉", "部门","部门负责⼈","经营值","收⼊", "⽀出","填报⼯时","标准⼯时","经营参数")); port(list,headList,"部门经营总览导出");
info = Commonutil.setInfo(info,"部门经营总览导出");
}catch (Exception e){
<(Class().getName()+".exportExcel()", e);
}
String();
}
⾸先,理解⼀下⼀个Excel的⽂件的组织形式,⼀个Excel⽂件对应于⼀个workbook(XSSFWorkbook),⼀个workbook可以有多个sheet(XSSFSheet)组成,⼀个sheet是由多个row(XSSFRow)组成,⼀个row是由多个cell(XSSFCell)组成。
public static <T> boolean export(List<T> list,List<String> headList,String fileName){
return myExport(list,headList,fileName,com.bronzesoft.rdm.platform.util.Constants.PATH + com.bronzesoft.power.platform.Constants.TEMPFOLDER_DIR ); }
//将list导出为excel,⽂件名为fileName
public static <T> boolean myExport(List<T> list,List<String> headList,String fileName,String path){
try{
if(!checkListAndHead(list,headList)){
LogUtil.info( "head的长度有问题,导出的⽂件不正确" );
}
File file = new File(path + File.separator + fileName + ".xlsx");
if(!ists()) {
}
//创建对应excel⽂件,存储路径path待确定
XSSFWorkbook workBook = new XSSFWorkbook();
//创建⼀个excel的sheet页
XSSFSheet sheet = ateSheet(fileName);
XSSFRow row = null;
XSSFCell cell = null;
Map<String, CellStyle> styles = ateStyles(workBook);
//表头样式
CellStyle headCenterIndex = ("headCenter");
//列的样式居中,背景颜⾊为⽩⾊
CellStyle center = ("centerWhite");
row = ateRow(0);
//表头数据
for (int i = 0; i < headList.size(); i++) {
//创建列
cell = ateCell(i);
//设置列的value
cell.(i));
//设置列的样式
cell.setCellStyle(headCenterIndex);
}
/
/表格数据
//写⼊表格数据
String codeName = "";
int rownum = 1;
T obj = null;
for(int i = 0; i < list.size(); i++){
obj = (i);
if(null != obj){
Field[] fields = Class().getDeclaredFields();
row = ateRow(rownum);
for(int j = 0; j< fields.length; j++){
fields[j].setAccessible(true);
codeName = String.valueOf(fields[j].get(obj));
//创建第j列
cell = ateCell(j);
cell.StringVal(codeName));
cell.setCellStyle(center);
}
}
rownum++;
}
// //i代表列,设置列的宽度
/
/ for (int i = 0; i < headList.size(); i++) {
// if(i == 0){
// sheet.setColumnWidth(i, 2000);
// }else if(i == 1 || i == 3){
// sheet.setColumnWidth(i, 6000);
// }else{
// sheet.setColumnWidth(i, 4000);
// }
// }
//将⽂件写到临时⽬录
FileOutputStream out = new FileOutputStream(file);
workBook.write(out);
}catch (Exception e){
<(fileName + "export失败" );
}
return true;
}
//检查head的size是否符合规范
public static <T> boolean checkListAndHead(List<T> list,List<String> headList){
if(list.size()>0){
T t = (0);
if(getColumnCount(t) != headList.size()){
LogUtil.info( "head的长度有问题" );
return false;
}
}
return true;
}
//获取⼀个对象成员变量的个数
public static <T> int getColumnCount(T t){
Field[] fields = t.getClass().getDeclaredFields();
int count = fields.length;
return count;
}
4.前端对接
public static JSONObject setInfo(JSONObject info,String fileName) throws Exception {
Storage s = DefaultStorage();
info.put("port", String.Port()));
info.put("dirAddress", de(com.bronzesoft.rdm.platform.util.Constants.PATH));
info.put("address", de(com.bronzesoft.power.platform.Constants.TEMPFOLDER_DIR + File.separator + fileName + ".xlsx")); info.put("name", de(fileName));
info.put("extendName", "xlsx");
return info;
}
5.前端代码
function doExport(){
synAjax.tabCall("com.bronzesoft.rdm.SeDeptTotalTab", "export", null, function(data){
doDownload(data);
java创建文件});
}
function doDownload(data){
console.log('data',data)
r = eval('('+data+')');
$("#_file_dir").val(r.dirAddress);
$("#_file_address").val(r.address);
$("#_file_name").val(r.name);
$("#_file_extname").dName);
if ($.browser.safari) { $("#_file_downform").attr("target", ""); }
var servlet = powerPath + "download";
$("#_file_downform").attr("action", servlet).submit();
}
到此这篇关于java中的export⽅法实现导出excel⽂件的⽂章就介绍到这了,更多相关java导出excel⽂件内容请搜索以前的⽂章或继续浏览下⾯的相关⽂章希望⼤家以后多多⽀持!

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。