java導(dǎo)出數(shù)據(jù)庫(kù)中Excel表格數(shù)據(jù)的方法
本篇文章基于java把數(shù)據(jù)庫(kù)中的數(shù)據(jù)以Excel的方式導(dǎo)出,歡迎各位大神吐槽:
1、基于maven jar包引入如下:
<dependency> <groupId>net.sourceforge.jexcelapi</groupId> <artifactId>jxl</artifactId> <version>2.6.12</version> </dependency>
2、首先創(chuàng)建數(shù)據(jù)庫(kù)對(duì)應(yīng)的實(shí)體類VO :UserVO(具體代碼省略);
3、確定導(dǎo)出Excel內(nèi)的title列,并放在數(shù)組里:String[] (具體代碼省略);
4、編寫(xiě)導(dǎo)出Excel的方法:
傳入?yún)?shù):
Excel名稱,Excel內(nèi)的title列數(shù)組String[],數(shù)據(jù)集合List<UserVO>
package bp.util;
import java.io.OutputStream;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.Field;
import jxl.Workbook;
import jxl.format.Alignment;
import jxl.format.Border;
import jxl.format.BorderLineStyle;
import jxl.format.VerticalAlignment;
import jxl.write.Label;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
public class ExportExcel {
public final static String exportExcel(String fileName, String[] Title, List listContent,
HttpServletResponse response) {
String result = "Excel文件導(dǎo)出成功!";
try {
OutputStream os = response.getOutputStream();
response.reset();
response.setHeader("Content-disposition",
"attachment; filename=" + new String(fileName.getBytes("GB2312"), "ISO8859-1"));
response.setContentType("application/msexcel");
WritableWorkbook workbook = Workbook.createWorkbook(os);
WritableSheet sheet = workbook.createSheet("Sheet1", 0);
jxl.SheetSettings sheetset = sheet.getSettings();
sheetset.setProtected(false);
WritableFont BoldFont = new WritableFont(WritableFont.ARIAL, 10, WritableFont.BOLD);
WritableCellFormat wcf_center = new WritableCellFormat(BoldFont);
wcf_center.setBorder(Border.ALL, BorderLineStyle.THIN);
wcf_center.setVerticalAlignment(VerticalAlignment.CENTRE);
wcf_center.setAlignment(Alignment.CENTRE);
wcf_center.setWrap(true);
for (int i = 0; i < Title.length; i++) {
sheet.setColumnView(i, 20);
sheet.addCell(new Label(i, 0, Title[i], wcf_center));
}
Field[] fields = null;
int i = 1;
for (Object obj : listContent) {
fields = obj.getClass().getDeclaredFields();
int j = 0;
for (Field v : fields) {
v.setAccessible(true);
Object va = v.get(obj);
if (va == null) {
va = "";
}
sheet.addCell(new Label(j, i, va.toString(), wcf_center));
j++;
}
i++;
}
workbook.write();
workbook.close();
} catch (Exception e) {
result = "Excel文件導(dǎo)出失敗";
e.printStackTrace();
}
return result;
}
}
在需要導(dǎo)出數(shù)據(jù)的時(shí)候調(diào)用此方法即可;
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
PostgreSQL Docker部署+SpringBoot集成方式
本文介紹了如何在Docker中部署PostgreSQL和pgadmin,并通過(guò)SpringBoot集成PostgreSQL,主要步驟包括安裝PostgreSQL和pgadmin,配置防火墻,創(chuàng)建數(shù)據(jù)庫(kù)和表,以及在SpringBoot中配置數(shù)據(jù)源和實(shí)體類2024-12-12
Java微信公眾平臺(tái)開(kāi)發(fā)(15) 微信JSSDK的使用
這篇文章主要為大家詳細(xì)介紹了Java微信公眾平臺(tái)開(kāi)發(fā)第十五步,微信JSSDK的使用方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-04-04
SpringCloud微服務(wù)之Config知識(shí)總結(jié)
今天帶大家學(xué)習(xí)SpringCloud微服務(wù)中的Config的相關(guān)知識(shí),文中有非常詳細(xì)的介紹,對(duì)正在學(xué)習(xí)SpringCloud微服務(wù)的小伙伴們有很好地幫助,需要的朋友可以參考下2021-05-05
詳解Spring bean的注解注入之@Autowired的原理及使用
之前講過(guò)bean注入是什么,也使用了xml的配置文件進(jìn)行bean注入,這也是Spring的最原始的注入方式(xml注入).本文主要講解的注解有以下幾個(gè):@Autowired、 @Service、@Repository、@Controller 、@Component、@Bean、@Configuration、@Resource ,需要的朋友可以參考下2021-06-06
java反射機(jī)制給實(shí)體類相同字段自動(dòng)賦值實(shí)例
這篇文章主要介紹了java反射機(jī)制給實(shí)體類相同字段自動(dòng)賦值實(shí)例,具有2020-08-08
maven打包上傳到私有倉(cāng)庫(kù)的實(shí)現(xiàn)步驟
這篇文章主要介紹了maven打包上傳到私有倉(cāng)庫(kù)的實(shí)現(xiàn)步驟,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-01-01
Java獲取json數(shù)組對(duì)象的實(shí)例講解
下面小編就為大家分享一篇Java獲取json數(shù)組對(duì)象的實(shí)例講解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-03-03
JAVA中ArrayList和數(shù)組的轉(zhuǎn)換與遇到的問(wèn)題解決
做研發(fā)的朋友都知道,在項(xiàng)目開(kāi)發(fā)中經(jīng)常會(huì)碰到ArrayList與數(shù)組類型之間的相互轉(zhuǎn)換,這篇文章主要給大家介紹了關(guān)于JAVA中ArrayList和數(shù)組的轉(zhuǎn)換與遇到的問(wèn)題解決,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-05-05
java中將漢字轉(zhuǎn)換成拼音的實(shí)現(xiàn)代碼
java中將漢字轉(zhuǎn)換成拼音的實(shí)現(xiàn)代碼。需要的朋友可以過(guò)來(lái)參考下,希望對(duì)大家有所幫助2013-10-10
Java基于elasticsearch實(shí)現(xiàn)集群管理
這篇文章主要介紹了java基于elasticsearch實(shí)現(xiàn)集群管理,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-02-02

