日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

java实现这个无表头空表格,《java程序导出excel表格是空白的没有数据?》 java怎么导入数据库...

發(fā)布時間:2023/12/20 数据库 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java实现这个无表头空表格,《java程序导出excel表格是空白的没有数据?》 java怎么导入数据库... 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

java程序導出excel表格是空白的沒有數(shù)據(jù)?

看一下是否可以鏈接上數(shù)據(jù)庫,或者查看一下鏈接的數(shù)據(jù)庫是否有數(shù)據(jù)

java從數(shù)據(jù)庫中導出excel poi

建議你使用pageoffice。

java web項目: 一個excel文件以二進制的形式存在數(shù)據(jù)庫中 如何將它導出并

poi即可。

/**

* 讀取Excel內(nèi)容

* @param InputStream

* @return Map 包含單元格數(shù)據(jù)內(nèi)容的Map對象

*/

public Map readExcelContent(InputStream is) {

Map content = new HashMap();

String str = "";

try {

fs = new POIFSFileSystem(is);

wb = new HSSFWorkbook(fs);

} catch (IOException e) {

e.printStackTrace();

}

sheet = wb.getSheetAt(0);

// 得到總行數(shù)

int rowNum = sheet.getLastRowNum();

row = sheet.getRow(0);

int colNum = row.getPhysicalNumberOfCells();

// 內(nèi)該從第二行開始,第一行為表頭的標題

for (int i = 1; i <= rowNum; i ) {

row = sheet.getRow(i);

int j = 0;

while (j < colNum) {

// 每個單元格的數(shù)據(jù)內(nèi)容用"-"分割開,以后需要時用String類的replace()方法還原數(shù)據(jù)

// 也可以將每個單元格的數(shù)據(jù)設置到一個javabean的屬性中,此時需要新建一個javabean

// str = getStringCellValue(row.getCell((short) j)).trim()

// "-";

str = getCellFormatValue(row.getCell((short) j)).trim() " ";

j ;

}

content.put(i, str);

str = "";

}

return content;

}

java中如何將數(shù)據(jù)庫中的數(shù)據(jù)導出到excel中(

項目中很多都會將導出到excel,然后對數(shù)據(jù)進理分析,在之前的項目中,多處用到能,也走了很多彎路,從一開始的tableExcel到現(xiàn)在的poi,從一開始用HSSFWorkbook

再到XSSFWorkbook,一步步優(yōu)化,廢話少說,直接開始.

項目框架

1,后臺:spring springmvc mybatis

2,前臺: bootstrap jQuery ajax

3,項目管理:maven

說明.excel處理函數(shù)需要引入poi的jar包,在pom.xml引入一下代碼

[html]?view plain?copy

org.apache.poi

poi

3.8

commons-codec

commons-codec

org.apache.poi

poi-ooxml

3.8

別的框架大體上也是可以的,只需稍微調(diào)整,如有問題,大家可留言討論

實現(xiàn)的功能說明:將數(shù)據(jù)庫中的人員信息(姓名.年齡,電話)導出到excel

具體代碼如下

1,前臺html代碼

[html]?view plain?copy

姓名:

性別:

請選擇性別

年齡:

導出

前臺頁面效果圖

2,js代碼

[javascript]?view plain?copy

var?User?=?function(){

this.init?=?function(){

//?用于導出excel

$("#userExport").click(function()?{

var?url?=??'/user/export/';

location.href?=?url? ?"?queryJson=" JSON.stringify(user.acquireInquireData());

});

};

//獲取查詢條件

this.acquireInquireData?=?function(){

var?inquireCondition?=?{

name:$('#name').val(),//名稱

sex:?$('#sex').val(),//性別

age:?$('#age').val(),//年齡

};

return?inquireCondition;

};

}

var?user;

$(function(){

user?=?new?User();

user.init();

});

3,domain的user實體類(該實體類如果作為最終導出結果的實體類,則該類的字段必須與需要導出的字段保持一致,且與導出列表的順序也得保持一致)

[java]?view plain?copy

/**

*?@author??李光光(編碼小王子)

*?@QQ??????826331692

*?@date????2016年11月7日?下午2:57:03

*?@version?1.0

*/

public?class?User?{

private??String?name;

private?String?sex;

private?String?age;

public?String?getName()?{

return?name;

}

public?void?setName(String?name)?{

this.name?=?name;

}

public?String?getSex()?{

return?sex;

}

public?void?setSex(String?sex)?{

this.sex?=?sex;

}

public?String?getAge()?{

return?age;

}

public?void?setAge(String?age)?{

this.age?=?age;

}

}

4,controller層代碼

[java]?view plain?copy

/**

*?@author?李光光(編碼小王子)

*?@date?2015年12月29日?下午4:04:00

*?@qq??826331692

*?@version?1.0

*?@return

*/

@Controller

@RequestMapping("/user")

public?class?UserController?{

@Autowired

private?UserService?userService;

/**

*?用于導出excel的查詢結果

*?@param?queryJson

*?@return

*/

@RequestMapping("/export")

public?void?export(HttpServletRequest?request,?HttpServletResponse?response,

@RequestParam(value?=?"queryJson")?String?queryJson)?{

User?user?=?JSON.parseObject(queryJson,?User.class);

List?userlList?=?userService.getUserForExcel(user);

ExportExcel?ee=?new?ExportExcel();

String[]?headers?=?{?"序號",?"姓名",?"性別",?"年齡"?};

String?fileName?=?"用戶信息表";

ee.exportExcel(headers,userlList,fileName,response);

}

}

5,service層代碼

[java]?view plain?copy

public?interface?UserService?{

/**

*?根據(jù)查詢條件查詢出所有的記錄,不用分頁,用于excel導出功能

*?@param?userDeviceVo

*?@return

*/

public?List?getUserDeviceForExcel(User?user);

}

6,service實現(xiàn)層代碼

[java]?view plain?copy

/**

*?@author??李光光(編碼小王子)

*?@date?2015年12月29日?下午3:43:08

*?@Email?826331692@qq.com

*?@version?1.0

*?@return

*/

@service

public?class?UserServiceImpl?implements?UserService?{

@Autowired

private?UserDao??userDao;

/**

*?根據(jù)查詢條件查詢出所有的記錄,不用分頁,用于excel導出功能

*?@param?userDeviceVo

*?@return

*/

@Override

public?List?getUserDeviceForExcel(User?user)?{

List?list?=?userDao.getUserForExcel(user);

Integer?order;

for?(int?i?=?0;?i?

order?=?i? ?1;

list.get(i).setOrder(order.toString());

if?(list.get(i).getSex().equals("1"))?{

list.get(i).setSex("男");

}?else?{

list.get(i).setSex("女");

}

}

return?list;

}

}

6dao層代碼

[java]?view plain?copy

public?interface?UserDao?{

/**

*?根據(jù)查詢條件查詢出所有的記錄,不用分頁,用于excel導出功能

*?@param?userDeviceVo

*?@return

*/

List?getUserForExcel(User?user);

}

需要在配置文件中加載UserDao的信息,否則無法找到UserDao,配置如下:

[html]?view plain?copy

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="

http://www.springframework.org/schema/beans?http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

${DB_MSSQL_DRIVER}

${DB_MSSQL_URL}

${DB_MSSQL_USER}

${DB_MSSQL_PW}

${maxActive}

${maxIdle}

${minIdle}

${maxWait}

${timeBetweenEvictionRunsMillis}

${minEvictableIdleTimeMillis}

${testWhileIdle}

${validationQuery}

${removeAbandoned}

${removeAbandonedTimeout}

${DB_MSSQL_CMS_DRIVER}

${DB_MSSQL_CMS_URL}

${DB_MSSQL_CMS_USER}

${DB_MSSQL_CMS_PW}

${maxActive}

${maxIdle}

${minIdle}

${maxWait}

${timeBetweenEvictionRunsMillis}

${minEvictableIdleTimeMillis}

${testWhileIdle}

${validationQuery}

${removeAbandoned}

${removeAbandonedTimeout}

7mybatis代碼

[html]?view plain?copy

select?name,sex,age

from?juser_table

where??1=1?and

and??name=#{name}

and??sex=#{sex}

and??age=#{age}

8,重頭戲來了,將List數(shù)據(jù)寫入到excel的代碼如下:

[java]?view plain?copy

package?com.jd.xe.web.service.userDevice;

import?java.io.BufferedOutputStream;

import?java.lang.reflect.Field;

import?java.lang.reflect.Method;

import?java.util.Collection;

import?java.util.Date;

import?java.util.Iterator;

import?javax.servlet.http.HttpServletResponse;

import?org.apache.poi.xssf.usermodel.XSSFCell;

import?org.apache.poi.xssf.usermodel.XSSFRichTextString;

import?org.apache.poi.xssf.usermodel.XSSFRow;

import?org.apache.poi.xssf.usermodel.XSSFSheet;

import?org.apache.poi.xssf.usermodel.XSSFWorkbook;

import?com.jd.xe.web.utils.DateUtil;

/**

*?@author??李光光(編碼小王子)

*?@Email???826331692@qq.com

*?@date????2016年7月18日?下午9:03:29

*?@version?1.0

*/

public?class?ExportExcel?{

public?void?exportExcel(String[]?headers,Collection?dataset,?String?fileName,HttpServletResponse?response)?{

//?聲明一個工作薄

XSSFWorkbook?workbook?=?new?XSSFWorkbook();

//?生成一個表格

XSSFSheet?sheet?=?workbook.createSheet(fileName);

//?設置表格默認列寬度為15個字節(jié)

sheet.setDefaultColumnWidth((short)?20);

//?產(chǎn)生表格標題行

XSSFRow?row?=?sheet.createRow(0);

for?(short?i?=?0;?i?

XSSFCell?cell?=?row.createCell(i);

XSSFRichTextString?text?=?new?XSSFRichTextString(headers[i]);

cell.setCellValue(text);

}

try?{

//?遍歷集合數(shù)據(jù),產(chǎn)生數(shù)據(jù)行

Iterator?it?=?dataset.iterator();

int?index?=?0;

while?(it.hasNext())?{

index ;

row?=?sheet.createRow(index);

T?t?=?(T)?it.next();

//?利用反射,根據(jù)javabean屬性的先后順序,動態(tài)調(diào)用getXxx()方法得到屬性值

Field[]?fields?=?t.getClass().getDeclaredFields();

for?(short?i?=?0;?i?

XSSFCell?cell?=?row.createCell(i);

Field?field?=?fields[i];

String?fieldName?=?field.getName();

String?getMethodName?=?"get"? ?fieldName.substring(0,?1).toUpperCase()? ?fieldName.substring(1);

Class?tCls?=?t.getClass();

Method?getMethod?=?tCls.getMethod(getMethodName,?new?Class[]?{});

Object?value?=?getMethod.invoke(t,?new?Object[]?{});

//?判斷值的類型后進行強制類型轉換

String?textValue?=?null;

//?其它數(shù)據(jù)類型都當作字符串簡單處理

if(value?!=?null

總結

以上是生活随笔為你收集整理的java实现这个无表头空表格,《java程序导出excel表格是空白的没有数据?》 java怎么导入数据库...的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。