一个将数据文件转换成excel文件打印的java实现方法的代码片断(Struts+poi)

页面文件得
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<%@ page language="java" contentType="text/html;charset=GBK" %>

<html:html>
<html:form method="post" action="/download/uploadExcel" enctype="multipart/form-data">
<html:button property="button" οnclick="printAll()">
DownLoad
</html:button>

<input type="button" name="excel" value="excel" οnclick="downloadexcel()" style="width:50">
</html:form>
</html:html>


<script language='javascript'>


function downloadexcel()
{
location.href="http://localhost:8080/downfile/download/excel.do";
}

</script>

JAVA文件得
action

package com.action;

import org.apache.struts.action.*;
import org.apache.struts.*;
import javax.servlet.http.*;
import java.io.*;
import com.bean.*;
import org.apache.log4j.*;
import org.apache.struts.upload.*;


/**
 * <p>Title: </p>
 *
 * <p>Description: </p>
 *
 * <p>Copyright: Copyright (c) 2004</p>
 *
 * <p>Company: </p>
 *
 * @author not attributable
 * @version 1.0
 */

public class DownFile extends Action {
   public static Logger logger = Logger.getLogger(DownFile.class);


    public ActionForward execute(ActionMapping mapping,
                                 ActionForm form,
                                 HttpServletRequest request,
                                 HttpServletResponse response) throws Exception {
        ActionForward myForward = null;
        try {

            String myAction = mapping.getParameter();
if ("EXCEL".equalsIgnoreCase(myAction)) {
                myForward = performDownExcel(mapping, form, request, response);
}
        } catch (Exception e) {
            throw e;
        }
        return myForward;
    }

    public ActionForward performDownTxt(ActionMapping mapping,
                                        ActionForm form,
                                        HttpServletRequest request,
                                        HttpServletResponse response) throws
            Exception {

        String webpath = System.getProperty("DOWNFILE.WEBPATH");
        System.out.println("webpath:" + webpath);
        System.out.println("LOG4J.PROPERTY" +
                           System.getProperty("LOG4J.PROPERTY"));
        logger.info(request.getServletPath());
        OutputStream os = response.getOutputStream(); //取得输出流
        //String  filename = contextPath + "/SystemErr.txt";
        //System.out.println(filename);

        try {
            System.out.println("success");
            logger.debug("success");
            String fname = "test"; //txt文件名
            response.reset(); //清空输出流
            response.setHeader("Content-disposition",
                               "attachment; filename=" + fname + ".txt"); //设定输出文件头
            response.setContentType("unknown"); //定义输出类型
            TxtBean tb = new TxtBean();
            tb.exportTxt(os); //调用生成txt文件bean
        } catch (Exception e) {
            System.out.println(e);
        } finally {
            os.close();
        }

        return mapping.findForward("display");
    }


    public ActionForward performDownExcel(ActionMapping mapping,
                                          ActionForm form,
                                          HttpServletRequest request,
                                          HttpServletResponse response) throws
            Exception {
        OutputStream os = response.getOutputStream(); //取得输出流
        try {
            String fname = "test"; //txt文件名
            response.reset(); //清空输出流

            response.setContentType("application/vnd.ms-excel"); //定义输出类型
            ExcelBean eb = new ExcelBean();
            eb.expordExcel(os); //调用生成Excel文件bean
            System.setOut(new PrintStream(os));
            os.flush();

        } catch (Exception e) {
            System.out.println(e);
        } finally {
            os.close();
        }
        return mapping.findForward("display");
    }

    public ActionForward performUpLoadExcel(ActionMapping mapping,
                                          ActionForm form,
                                          HttpServletRequest request,
                                          HttpServletResponse response) throws
            Exception {
        System.out.println("dafdsafsa");
        ExcelForm  excelForm = (ExcelForm)form; //取得输出流


        try {
            FormFile file =  excelForm.getExcelfile();
            System.out.println("------"+new String(file.getFileName().getBytes("ISO8859_1"),"GB2312"));
            jxl.Workbook wb = jxl.Workbook.getWorkbook(file.getInputStream());

            jxl.Sheet sheet =  wb.getSheet(0);
            System.out.println("ok"+sheet.getCell(0,0).getContents());
        } catch (Exception e) {
            System.out.println(e);
        }
        return mapping.findForward("display");
    }
}

处理生产Excel得代码
package com.bean;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFDataFormat;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.hssf.usermodel.HSSFFooter;
import org.apache.poi.hssf.usermodel.HSSFHeader;
import java.io.*;


public class ExcelBean {

    public void expordExcel(OutputStream os) throws Exception{

        HSSFWorkbook wb = new HSSFWorkbook();

        HSSFSheet sheet = wb.createSheet("new sheet");
        HSSFHeader header = sheet.getHeader();

        header.setCenter("工资报表");

        HSSFRow row1 = sheet.createRow((short) 0);

        HSSFCell cell11 = row1.createCell((short) 0);

        cell11.setEncoding(HSSFCell.ENCODING_UTF_16);

        cell11.setCellValue("编号");

        HSSFCell cell12 = row1.createCell((short) 1);

        cell12.setEncoding(HSSFCell.ENCODING_UTF_16);

        cell12.setCellValue("部门");

        HSSFCell cell13 = row1.createCell((short) 2);

        cell13.setEncoding(HSSFCell.ENCODING_UTF_16);

        cell13.setCellValue("姓名");

        HSSFCell cell14 = row1.createCell((short) 3);

        cell14.setEncoding(HSSFCell.ENCODING_UTF_16);

        cell14.setCellValue("应发工资");

        HSSFCell cell15 = row1.createCell((short) 4);

        cell15.setEncoding(HSSFCell.ENCODING_UTF_16);

        cell15.setCellValue("基本工资");

        HSSFCell cell16 = row1.createCell((short) 5);

        cell16.setEncoding(HSSFCell.ENCODING_UTF_16);

        cell16.setCellValue("岗位工资");

        HSSFCell cell17 = row1.createCell((short) 6);

        cell17.setEncoding(HSSFCell.ENCODING_UTF_16);

        cell17.setCellValue("奖金");

        sheet.setGridsPrinted(true);

        HSSFFooter footer = sheet.getFooter();

        footer.setRight("Page " + HSSFFooter.page() + " of " +

                        HSSFFooter.numPages());
        wb.write(os);

    }


    public static void main(String[] args) {

        FileOutputStream fos = null;
        try {
            ExcelBean eb = new ExcelBean();
            fos = new FileOutputStream("test.xls");
            eb.expordExcel(fos);

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                fos.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值