java 將查詢的資料匯出成excel檔案

2021-09-13 20:31:53 字數 2929 閱讀 8594

controller 層**

/**

* 匯出excel

* @param globalid

* @param createtime

*/public void findsigndataexport(string globalid, string createtime, httpservletrequest request, httpservletresponse response)

serviceimpl 層**  

/**

* 根據 globalid、建立日期,查詢報名資訊並匯出excel

}// 獲取報名完整資訊

/*** 將資料用新的map接收

*/iterator> iterator = entries.iterator();

mapmap = new hashmap<>(16);

while(iterator.hasnext())}}

}list> list = new arraylist<>();

listtitlename = new arraylist<>();

int index = 0;

if(i == 0)}}

mapnewmap = new hashmap<>(16);

set> entries = stringstringmap.entryset();

iterator> iterator = entries.iterator();

while (iterator.hasnext())

list.add(newmap);

}for (int i = 0; i < titlename.size(); i++ )}}

// excel標題

string title = new string[titlename.size()];

// 匯出測評的結果

titlename.toarray(title);

// title = new string ;

// 測評名稱、測評時間

string evaluationname = "";

string evaluationtime = "";

string content = new string[list.size()][title.length];

for (int i = 0; i < list.size(); i++)

}// excel檔名

string filename = "報名列表" + system.currenttimemillis() + ".xls";

// sheet名

string sheetname = evaluationname + "_" + evaluationtime.split(" ")[0];

// 建立hssfworkbook

hssfworkbook wb = excelutil.gethssfworkbook(sheetname, title, content, null);

// 響應到客戶端

try catch (exception e)

}

這個是抽取出來的公共方法 

// 傳送響應流方法

}

接下來是工具類

package tide.jstv.operationdata.utils;

import org.apache.poi.hssf.usermodel.*;

public class excelutil

// 第二步,在workbook中新增乙個sheet,對應excel檔案中的sheet

hssfsheet sheet = wb.createsheet(sheetname);

// 第三步,在sheet中新增表頭第0行,注意老版本poi對excel的行數列數有限制

hssfrow row = sheet.createrow(0);

// 第四步,建立單元格,並設定值表頭 設定表頭居中

hssfcellstyle style = wb.createcellstyle();

style.setalignment(hssfcellstyle.align_center); // 建立乙個居中格式

// 宣告列物件

hssfcell cell = null;

// 建立標題

for (int i = 0; i < title.length; i++)

// 建立內容

for (int i = 0; i < values.length; i++)

}return wb;

}}

到這裡就已經完成了

使用GridView將資料匯入或匯出Excel中

preclass csharp name code using system using system.collections using system.configuration using system.data using system.linq using system.web using ...

將查詢的記錄匯出成excel表(poi)

poi主要物件 poi物件名稱 對應的excel物件 hssfworkbook 工作簿hssfsheet 工作表hssfrow 行hssfcell 單元格 poi與將查詢結果集組合一起使用,具體 如下 public static void resultsettoexcel resultset rs,...

vc 將LISTCTRL裡的內容儲存成EXCEL

1.獲取當前時間做excel的檔名 word syear,smonth,sday,shour,sminute,ssecond systemtime curtime 定義systemtime類的物件 getlocaltime curtime 獲取本地時間 syear curtime.wyear 獲得年...