现在很多网页啊,app啊,为了提高用户体验度,方便用户清晰可以随时能够看到想要的数据都会添加一个导出数据功能,把想要的数据统一导出到一个excel中。我也是听一个朋友偶然说的,昨天就刷了刷各种微博,基本都说的差不多,主要靠自己动手,这不,终于搞出来了。先上图,瞅瞅。
进入正题
一,做这个导出数据必要的依赖(我这里是ssm的maven项目)
<!--excel导出 -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactI
<version>3.10-FINAL</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.10-FINAL</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml-schemas</artifactId>
<version>3.10-FINAL</version>
</dependency>
二,需要的工具类
public class POIUtil {
public static void Excel2003Operate(String filePath) throws Exception {
HSSFWorkbook hssfWorkbook = new HSSFWorkbook(new FileInputStream(new File(filePath)));
HSSFSheet sheet = hssfWorkbook.getSheetAt(0);
for (int i = 0; i < 10000; i++) {
HSSFRow hssfRow = sheet.createRow(i);
for (int j = 0; j < 10; j++) {
HSSFCellUtil.createCell(hssfRow, j, String.valueOf(Math.random()));
}
}
FileOutputStream out = new FileOutputStream("workbook.xlsx");
hssfWorkbook.write(out);
out.close();
}
public static void ExcelOperate(String filePath) throws Exception {
Workbook workbook = WorkbookFactory.create(new FileInputStream(new File(filePath)));
Sheet first = workbook.getSheetAt(0);
for (int i = 0; i < 100000; i++) {
Row row = first.createRow(i);
for (int j = 0; j < 11; j++) {
if(i == 0) {
row.createCell(j).setCellValue("column" + j);
} else {
if (j == 0) {
row.createCell(j).setCellValue(i);
} else
row.createCell(j).setCellValue(Math.random());
}
}
}
FileOutputStream out = new FileOutputStream("workbook.xlsx");
workbook.write(out);
out.close();
}
public static void Excel2007AboveOperateOld(String filePath) throws IOException {
XSSFWorkbook workbook = new XSSFWorkbook(new FileInputStream(new File(filePath)));
// 获取第一个表单
Sheet first = workbook.getSheetAt(0);
for (int i = 0; i < 100000; i++) {
Row row = first.createRow(i);
for (int j = 0; j < 11; j++) {
if(i == 0) {
// 首行
row.createCell(j).setCellValue("column" + j);
} else {
// 数据
if (j == 0) {
CellUtil.createCell(row, j, String.valueOf(i));
} else
CellUtil.createCell(row, j, String.valueOf(Math.random()));
}
}
}
// 写入文件
FileOutputStream out = new FileOutputStream("workbook.xlsx");
workbook.write(out);
out.close();
}
/**
* 测试写入百万条数据
* <br/>Cast time : 87782
* @param filePath 文件路径
* @throws IOException
*/
public static void Excel2007AboveOperate(String filePath) throws IOException {
XSSFWorkbook workbook1 = new XSSFWorkbook(new FileInputStream(new File(filePath)));
SXSSFWorkbook sxssfWorkbook = new SXSSFWorkbook(workbook1, 100);
Sheet first = sxssfWorkbook.getSheetAt(0);
for (int i = 0; i < 10000; i++) {
Row row = first.createRow(i);
for (int j = 0; j < 11; j++) {
if(i == 0) {
// 首行
row.createCell(j).setCellValue("column" + j);
} else {
// 数据
if (j == 0) {
CellUtil.createCell(row, j, String.valueOf(i));
} else
CellUtil.createCell(row, j, String.valueOf(Math.random()));
}
}
}
FileOutputStream out = new FileOutputStream(filePath);
sxssfWorkbook.write(out);
out.close();
}
public static void MathRandomCastTime() {
long beginTime = System.currentTimeMillis();
for (int i = 0; i < 10000000; i++) {
Math.random();
}
long endTime = System.currentTimeMillis();
}
}
我在这里只用了其中一个方法,没引用这个类,贴出来看谁能用就用吧。(这个工具类也是别人的,不知道是谁的了,首先谢谢了,很实用)
三,通过js一个按钮导出数据
function exportList() {
$.ajax({
url: "/user/system/exportList",
dataType: "text",
type: "post",
cache: false,
success: function(obj) {
if (obj == "success") {
layer.alert('导出成功!', function(index) {
window.location.reload();
});
} else {
layer.alert("导出失败!");
}
},
error: function(textStatus, e) {
layer.alert("系统ajax交互错误: ");
}
});
}
后台处理
@RequestMapping( value = "exportList", method = RequestMethod.POST )
@ResponseBody
public String exportList( String cpage ) throws IOException
{
long beginTime = System.currentTimeMillis();
String filePath = "d://test.xlsx"; //excel存放的位置
XSSFWorkbook workbook = new XSSFWorkbook( new FileInputStream( new File( filePath ) ) );
SXSSFWorkbook sxssfWorkbook = new SXSSFWorkbook( workbook, 100 );
Sheet first = sxssfWorkbook.getSheetAt( 0 );
try {
List<Drug> dList = drugService.queryDruglist();
for ( int i = 0; i < dList.size(); i++ )
{
Row row = first.createRow( i );
for ( int j = 0; j < 10; j++ )
{
if ( i == 0 )
{
/* 首行 */
row.createCell( j ).setCellValue( "标题" + j );
} else {
/* 数据 */
if ( j == 0 ) //第一列
{
CellUtil.createCell( row, j, String.valueOf( i ) );
}else if ( j == 1 )//第二列
{
CellUtil.createCell( row, j, String.valueOf( dList.get( i ).getDrugs_id() ) );
}else if ( j == 2 )
{
CellUtil.createCell( row, j, String.valueOf( dList.get( i ).getPro_num() ) );
}else if ( j == 3 )
{
CellUtil.createCell( row, j, String.valueOf( dList.get( i ).getDrugs_name() ) );
}else if ( j == 4 )
{
CellUtil.createCell( row, j, String.valueOf( dList.get( i ).getFactory() ) );
}else if ( j == 5 )
{
CellUtil.createCell( row, j, String.valueOf( dList.get( i ).getHa_level() ) );
}else if ( j == 6 )
{
CellUtil.createCell( row, j, String.valueOf( dList.get( i ).getPack() ) );
}else if ( j == 7 )
{
CellUtil.createCell( row, j, String.valueOf( dList.get( i ).getFactory() ) );
}
}
}
}
FileOutputStream out = new FileOutputStream( filePath );
sxssfWorkbook.write( out );
out.close();
long endTime = System.currentTimeMillis();
return("success");
} catch ( Exception e ) {
();
}
return("fail");
}
可以尝试一下。