java实现下载带汉字的文件

 我所用的是POI可在www.apace.org上下载

下面是代码

 

package  com.javawebdemo.struts.common.excel;

import  java.io.ByteArrayOutputStream;
import  java.io.FileInputStream;
import  java.io.FileOutputStream;
import  java.net.URLEncoder;
import  java.util.ArrayList;
import  java.util.List;

import  javax.servlet.ServletOutputStream;
import  javax.servlet.http.HttpServletResponse;

import  org.apache.commons.logging.Log;
import  org.apache.commons.logging.LogFactory;
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.apache.poi.poifs.filesystem.POIFSFileSystem;

import  com.javawebdemo.struts.exception.AppException;

public   class  MakeExcelByPoi  {

    
private String readFilePath = "E:/Test/Test.xls";

    
private Log log = LogFactory.getLog(this.getClass().getName());
    
private HSSFWorkbook wb = null;

    
private POIFSFileSystem fs = null;

    
private HSSFSheet sheet = null;

    
private String CONTENT_TYPE = "application/vnd.ms-excel";

    
private String creatorName = "";

    
private String createdDate = "";

    
private String errorMessage = "";

    
private List listBean = null;

    
public MakeExcelByPoi() {
    }


    
public MakeExcelByPoi(String _creatorName, String _createdDate,
            List _listBean) 
{
        
this.creatorName = _creatorName;
        
this.createdDate = _createdDate;
        
this.listBean = _listBean;
    }


    
public String getCreatorName() {
        
return this.creatorName;
    }


    
public String getCreatedDate() {
        
return this.createdDate;
    }


    
public List getListBean() {
        
return this.listBean;
    }


    
public String getMessage() {
        
return this.errorMessage;
    }


    
public void setCreatorName(String _creatorName) {
        
this.creatorName = _creatorName;
    }


    
public void setCreatedDate(String _createdDate) {
        
this.createdDate = _createdDate;
    }


    
public void setListBean(List _listBean) {
        
this.listBean = _listBean;

    }


    
public void setMessage(String _errorMessage) {
        
this.errorMessage = _errorMessage;
    }


    
public void execute(HttpServletResponse response) throws AppException {
        log.info(
"execute is begin");
        
try {
            fs 
= new POIFSFileSystem(new FileInputStream(this.readFilePath));
            wb 
= new HSSFWorkbook(fs);
            sheet 
= wb.getSheetAt(0);
            writeAutoCells();
            writeManualCells();
            response.setContentType(CONTENT_TYPE);
//            response.setHeader("Content-Disposition",
//                    "inline; filename=tempXls.xls");
            String fileName = URLEncoder.encode("用户表.xls""UTF-8");
            response.setHeader(
"Content-Disposition""attachment; filename = "+
fileName);
            ServletOutputStream out 
= response.getOutputStream();
            ByteArrayOutputStream Byteout 
= new ByteArrayOutputStream();
            FileOutputStream fileOut 
= new FileOutputStream("workbook.xls");
            wb.write(fileOut);
            fileOut.close();
            wb.write(Byteout);
            response.setContentLength(Byteout.size());
            Byteout.writeTo(out);
            out.flush();
            log.debug(
"execute is executed");
        }
 catch (Exception ex) {
            log.info(ex.toString());
//            throw new AppException();
//throws ex;
        }
 finally{

        }

    }


    
public void writeAutoCells() {
        
try {
            setCellValue(
35this.getCreatorName());
            setCellValue(
45this.getCreatedDate());
        }
 catch (Exception ex) {
            System.out.println(ex.toString());
        }

    }


    
public void writeManualCells() {
        
try {
            
for (int j = 0; j < listBean.size(); j++{
                TestBean temBean 
= (TestBean) listBean.get(j);
                setCellValue(j 
+ 70, temBean.getTitle());
                setCellValue(j 
+ 71, temBean.getTypeName());
                setCellValue(j 
+ 72, temBean.getPlaceName());
                setCellValue(j 
+ 73, temBean.getDate());
                setCellValue(j 
+ 74, temBean.getStartTime());
                setCellValue(j 
+ 75, temBean.getEndTime());
            }

        }
 catch (Exception ex) {
            System.out.println(ex.toString());
        }

    }


    
private void setCellValue(int row, int line, String value) {
        HSSFRow rows 
= sheet.getRow(row);
        
if (rows == null{
            rows 
= sheet.createRow(row);
        }

        HSSFCell cell 
= rows.getCell((short) line);
        
if (cell == null{
            cell 
= rows.createCell((short) line);
        }

        cell.setEncoding(HSSFCell.ENCODING_UTF_16);
        cell.setCellValue(value);
    }


    
private void executeTest() {
        
try {
            fs 
= new POIFSFileSystem(new FileInputStream(this.readFilePath));
            wb 
= new HSSFWorkbook(fs);
            sheet 
= wb.getSheetAt(0);
            writeAutoCells();
            writeManualCells();
            FileOutputStream fileOut 
= new FileOutputStream("c:/workbook.xls");
            wb.write(fileOut);
            fileOut.close();
        }
 catch (Exception ex) {
            System.out.println(ex.toString());
        }

    }


    
public static void main(String[] args) throws Exception {
        MakeExcelByPoi printM 
= new MakeExcelByPoi();
        printM.setCreatorName(
"LZT");
        printM.setCreatedDate(
"2006/08/23");
        List List 
= new ArrayList();
        
for (int j = 0; j < 3; j++{
            TestBean temList 
= new TestBean();
            temList.setTitle(
"This is a Test !");
            temList.setTypeName(
"titleData");
            temList.setPlaceName(
"cnTypeNameData");
            temList.setDate(
"cnStageNameData");
            temList.setStartTime(
"");
            temList.setEndTime(
"");
            List.add(temList);
        }

        printM.setListBean(List);
        printM.executeTest();
    }


}

package  com.javawebdemo.struts.common.excel;

public   class  TestBean  {
    
private int id;
    
private int type_id;
    
private String title;
    
private String date;
    
private String startHour;
    
private String endHour;
    
private String startMinute;
    
private String endMinute;
    
private int placeId;
    
private String note;
    
private int isImportant;
    
private int isOutCompany;
    
private long topic_id;
    
private String startPoint;
    
private String endPoint;
    
private String deleteUrl;
    
private String typeName;
    
private String placeName;
    
private String view;
    
private String save_MainDate;
    
private String save_SideDate;
    
private String startTime;
    
private String endTime;
    
public int getId(){
        
return id;
    }


    
public int getType_id(){
        
return type_id;
    }


    
public String getTitle(){
        
return title;
    }


    
public String getDate(){
        
return date;
    }


    
public String getStartHour(){
        
return startHour;
    }


    
public String getEndHour(){
        
return endHour;
    }


    
public String getStartMinute(){
        
return startMinute;
    }


    
public String getEndMinute(){
        
return endMinute;
    }


    
public int getPlaceId(){
        
return placeId;
    }


    
public String getNote(){
        
return note;
    }


    
public int getIsImportant(){
        
return isImportant;
    }


    
public int getIsOutCompany(){
        
return isOutCompany;
    }


    
public long getTopic_id(){
        
return topic_id;
    }

    
public String getStartPoint(){
        
return startPoint;
    }

    
public String getEndPoint(){
        
return endPoint;
    }

    
public String getTypeName(){
        
return typeName;
    }

    
public String getPlaceName(){
        
return placeName;
    }

    
public String getView(){
        
return view;
    }

    
public String getSave_MainDate(){
        
return save_MainDate;
    }

    
public String getSave_SainDate(){
        
return save_SideDate;
    }

    
public String getStartTime(){
        
return startTime;
    }

    
public String getEndTime(){
        
return endTime;
    }


    
public void setEndTime(String _endTime){
        
this.endTime = _endTime;
    }

    
public void setStartTime(String _startTime){
        
this.startTime = _startTime;
    }

    
public void setSave_SideDate(String _save_SideDate){
        
this.save_SideDate=_save_SideDate;
    }

    
public void setSave_MainDate(String _save_MainDate){
        
this.save_MainDate=_save_MainDate;
    }

    
public void setView(String _view){
        
this.view=_view;
    }

    
public void setTypeName(String _typeName){
        
this.typeName=_typeName;
    }

    
public void setPlaceName(String _placeName){
        
this.placeName=_placeName;
    }

    
public void setStartPoint(String _startPoint){
        
this.startPoint=_startPoint;
    }

    
public void setEndPoint(String _endPoint){
        
this.endPoint=_endPoint;
    }

    
public void setId(int _id){
        
this.id = _id;
    }


    
public void setType_id(int _type_id){
        
this.type_id = _type_id;
    }


    
public void setTitle(String _title){
        
this.title = _title;
    }


    
public void setDate(String _date){
        
this.date = _date;
    }


    
public void setStartHour(String _startHour){
        
this.startHour = _startHour;
    }


    
public void setEndHour(String _endHour){
        
this.endHour = _endHour;
    }


    
public void setStartMinute(String _startMinute){
        
this.startMinute=_startMinute;
    }


    
public void setEndMinute(String _endMinute){
        
this.endMinute=_endMinute;
    }


    
public void setPlaceId(int _placeId){
        
this.placeId = _placeId;
    }


    
public void setNote(String _note){
        
this.note = _note;
    }


    
public void setIsImportant(int _isImportant){
        
this.isImportant = _isImportant;
    }


    
public void setIsOutCompany(int _isOutCompany){
        
this.isOutCompany = _isOutCompany;
    }


    
public void setTopic_id(long _topic_id){
        
this.topic_id = _topic_id;
    }




    
public void setDeleteUrl(String deleteUrl)
    
{
        
this.deleteUrl = deleteUrl;
    }

    
public String getDeleteUrl()
    
{
     
return deleteUrl;
     }



}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值