使用poi从服务器保存数据到本地


import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.util.List;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

/**
 * 用户信息导出类
 *
 * @author 寇航艇
 *
 */
public class UseServlet extends HttpServlet
{

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
    {
        doPost(request, response);
    }

    @SuppressWarnings("unchecked")
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
    {

        response.setContentType("text/html");
        // 封装页面的参数
        UserInfo bo = new UserInfo();

        // 调用业务层
        ServletContext servletContext = this.getServletContext();
        WebApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(servletContext);
        IUsersBus userinfo= (IUsersBus)ctx.getBean("userinfo");

        List<UserInfo> list = certUseDS.queryUses(bo, 50000, 1).getRows();
        OutputStream outs = response.getOutputStream();

        BufferedWriter bw = null;
        OutputStreamWriter osw = null;
        String fileName = "excel" + String.valueOf(System.currentTimeMillis()).substring(9, 13) + ".xls";
        String headStr = "attachment; filename=\"" + fileName + "\"";
        response.setContentType("APPLICATION/OCTET-STREAM");
        response.setHeader("Content-Disposition", headStr);
        osw = new OutputStreamWriter(outs, "GB2312");
        createExcel(list,outs);

    }

    /**
     * 将读取到的文件重新写入新的文件
     * @param output
     *
     * @param bw
     */
    public void createExcel( List<UserInfo> list, OutputStream output)
    {
        Workbook workBook = null;
        Row row = null;
        Cell cell = null;
        try
        {
            // 创建一个工作簿
            // 生成xlsx格式
            // workBook = new XSSFWorkbook();
            // 生成xls格式
            workBook = new HSSFWorkbook();
            // 创建工作表
            Sheet sheet = workBook.createSheet("users");
            // 创建excel表头
            createExcelHead(sheet.createRow(0));

            // 将数据写入excel
            for (int i = 0; i < list.size(); i++)
            {
                // 创建一行
                row = sheet.createRow(i + 1);
                int cellNo = 0;
                // 循环创建单元格
                cell = row.createCell(cellNo++);
                // 给单元格设值
                cell.setCellValue(list.get(i).getUserName());
                cell = row.createCell(cellNo++);
                cell.setCellValue(list.get(i).getBirth());
                cell = row.createCell(cellNo++);
                cell.setCellValue(list.get(i).getCreatedTime());
                cell = row.createCell(cellNo++);
                cell.setCellValue(list.get(i).getCounty());
             
            }
            // 将内容写入Excel
            workBook.write(output);
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        finally
        {
            try
            {
                output.close();
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
        }

    }

    /**
     * 创建excel表头
     *
     * @param row
     *            行
     */
    private void createExcelHead(Row row)
    {
        Cell cell;
        int cellNum = 0;
        // 设置标题
        cell = row.createCell(cellNum++);
        cell.setCellValue("用户名");
        cell = row.createCell(cellNum++);
        cell.setCellValue("生日");
        cell = row.createCell(cellNum++);
        cell.setCellValue("注册日期");
        cell = row.createCell(cellNum++);
        cell.setCellValue("籍贯");
 
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java POI 是一个用于操作 Microsoft Office 文档的开源 Java 库。使用 Java POI,我们可以生成和操作 Excel、Word 和 PowerPoint 等文档。 在批量生成模板数据时,可以通过以下步骤来实现: 首先,需要创建一个 Excel 模板文件,可以手动创建或使用已有的 Excel 文件。将需要填充数据的位置设置为特定的标记或占位符,例如使用特定的单元格标记或命名区域来表示需要填充的数据位置。 然后,使用 Java 代码中的 POI 库读取模板文件。可以使用 HSSFWorkbook 或 XSSFWorkbook 类来读取和处理 Excel 文件,取决于文件的格式。例如使用 XSSFWorkbook 对象来读取 .xlsx 格式的 Excel 文件。 接下来,根据模板中已定义的数据位置,使用 POI 库来进行模板数据的批量填充。可以使用 HSSFSheet 或 XSSFSheet 对象来获取工作表,并使用特定的方法来插入或填充数据。例如,使用行号和列号来获取特定单元格,然后使用setCellValue() 方法设置单元格的值。 在填充完所有数据后,可以选择将生成的新文件保存到指定的位置,或直接在内存中进行进一步处理。 最后,根据需求执行相应的操作,例如保存文件到本地磁盘、发送文件给用户或将文件传输至远程服务器等。 总结起来,使用 Java POI 可以很方便地实现批量生成模板数据的功能,通过读取和操作模板文件,按照预定的数据位置进行填充,并保存或处理生成的新文件。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值