基于SpringMvc的数据库表格导出

3 篇文章 0 订阅
3 篇文章 0 订阅

在项目开发过程中,用到了基于用户操作的数据库表格导出,记录下来,以备下次使用:

后台代码实现(基于SpringMvc):

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.chengpai.person.bean.Person;
import com.chengpai.person.bean.bumenMidObject;
import com.chengpai.person.service.PersonService;

import util.Mobile;
import util.Upload;

@Controller
public class PersonController {
@RequestMapping(value = "testex.do", method = RequestMethod.GET)
	@ResponseBody
	public Mobile ll(HttpServletRequest request, HttpServletResponse response) {
		response.setContentType("text/html;charset=UTF-8");
		Person p = new Person();
		p.setId(0);
		p.setName("zhangsan");
		p.setPassword("123");
		Person p1 = new Person();
		p1.setId(1);
		p1.setName("zhangsansan");
		p1.setPassword("1234");
		List<Person> list = new ArrayList<Person>();
		list.add(p);
		list.add(p1);
		// 创建一个excel文件
		HSSFWorkbook demoexcel = new HSSFWorkbook();
		// 创建Sheet对象
		HSSFSheet demoSheet = demoexcel.createSheet("用户列表1");
		// 创建excel文件标题
		String[] tableHeader = { "用户编号", "用户名", "用户密码" };

		// 创建行的对象
		HSSFRow row = demoSheet.createRow(0);// 参数为行下标,下标从0开始
		// 创建单元格
		for (int i = 0; i < tableHeader.length; i++) {
			HSSFCell cell = row.createCell(i);// 在一行中创建N个单元格,单元格下标从0开始
			// 将标题信息添加入单元格
			cell.setCellValue(tableHeader[i]);
		}

		for (int i = 0; i < list.size(); i++) {
			HSSFRow row1 = demoSheet.createRow(i + 1);

			HSSFCell cell1 = row1.createCell(0);
			cell1.setCellValue(list.get(i).getId());

			HSSFCell cell2 = row1.createCell(1);
			cell2.setCellValue(list.get(i).getName());

			HSSFCell cell3 = row1.createCell(2);
			cell3.setCellValue(list.get(i).getPassword());

		}

		// 设置以网格的形式输出对象
		demoSheet.setGridsPrinted(true);
		String filename = "aaa用户信息.xls";
		try {
			response.addHeader("Content-DisPosition", "attachment;filename=" + URLEncoder.encode(filename, "UTF-8"));

		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		// 将内存中的对象写入到硬盘
		try {
			OutputStream os = response.getOutputStream();
			demoexcel.write(os);
			os.flush();
			os.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		boolean a = true;
		Mobile mo = null;
		if (a) {
			mo = new Mobile("1", "添加成功", null);
		} else {
			mo = new Mobile("0", "添加失败", null);
		}
		return mo;
	}
}
前台H5界面的实现,(ajax触发请求):

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script type="text/javascript" src="js/jquery.min.js"></script>
<title>文件上传</title>
<script type="text/javascript">
$(document).ready(function () {
    $("#btnImportOK").click(function () {

        var formData = new FormData($("#uploadForm")[0]);
        $.ajax({
            type: "POST",
            data: formData,
            url: "testex.do",
            contentType: false,
            processData: false,
        }).success(function (data) {
            if (data.status) {
                console.log(data.url);
            } else {
                console.log(data.msg);
            }

        }).error(function (data) {
            alert(data);
            console.log(data);
        });

    });
});
</script>
</head>
<body>
<a href="testex.do" >下载</a>
</body>
</html>

打开网页,浏览器会自动打开文件保存路径。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值