乔山办公网我们一直在努力
您的位置:乔山办公网 > excel表格制作 > <em>java</em>如何另存<em>导出Excel</em>-ja

<em>java</em>如何另存<em>导出Excel</em>-ja

作者:乔山办公网日期:

返回目录:excel表格制作


导出636f7079e79fa5e98193361
现在我们定义好对象了,如何导出Excel --ExcelExportUtil 这个导出工具类
public void testExportExcel_1() throws Exception {
ExportParams params = new ExportParams("0328课程表", "日期:2016-03-28", "六年一班");
Workbook workbook = ExcelExportUtil.exportExcel(params, CourseEntity.class,courseList);
FileOutputStream fos = new FileOutputStream("D:/excel/0328课程表.xls");
workbook.write(fos);
fos.close();
}
我们只要把我们定义好的对象的class传进去,以及对象的集合,Easypoi就可以返回一个Excel的workbook了,同时Easypoi是兼容03版本office和07版本office,你要穿个参数指定下类型及可以了,是不是不是比我们自己写代码简单多了,最少只需要2行代码就可以完成我们的office操作了

导入
我们把导出写完了,导入是不是很复杂呢,也不是,导入也是同样简单,定时实体和上面定义的方式一样
导入是用导入工具类
ImportParams params = new ImportParams();
params.setHeadRows(2);
List<CourseEntity> list = ExcelImportUtil.importExcel(inputStream, CourseEntity.class, params);

定义下表头的参数,然后把流传入进去就可以得到我们的对象列表是不是so easy.赶快来使用吧

1./**

* 出险信息导出到excel(fc)

* @param mapping

* @param form

* @param request

* @param response

* @throws IOException

*/

public void exportActoExcel(ActionMapping mapping, ActionForm form ,

HttpServletRequest request,HttpServletResponse response) throws IOException {

ActionErrors errors = new ActionErrors();

AcExcelBusi acBusi = new AcExcelBusi();

AccidentRecordForm arForm= (AccidentRecordForm) form;

AccidentRecordBusi arBusi = new AccidentRecordBusi();

// ////查询条件

FwUsers sessUser = (FwUsers)request.getSession().getAttribute(ConstValues.SESS_USER_MANAGE);

Map<String,Object> cisMap = arBusi.getTodoPageList(arForm,sessUser,errors);

List AcList = null;// 当页的记录

if (null != cisMap) {

AcList = (List) cisMap.get("list");

}


//导出excel的路径、文件名

String uuid = UUID.create("exp");

String path = request.getSession().getServletContext().getRealPath("/") + ConstValues.EXP_PATH_EXCEL + uuid + ".xls";

acBusi.exprotAcExcel(AcList, path,request);


response.sendRedirect("stdownload.jsp?path=" + path );


}

2./**

* 导出出险信息 fc

* @param jzForm

*            查询条件

* @param sessionUser

*            当前登录session用户

* @param errors

*            Action错误

* @return

*/

public void exprotAcExcel(List list, String path,HttpServletRequest request) {


try {

WritableWorkbook workbook = Workbook.createWorkbook(new File(path));// 创建工作簿e799bee5baa6e79fa5e98193e4b893e5b19e339(filePos为excel文件的路径)

WritableSheet sheet = workbook.createSheet("出险信息", 0);// 创建工作页


// 内容(居中)单元格样式

WritableCellFormat contentStyle = new WritableCellFormat(); //

contentStyle.setAlignment(jxl.format.Alignment.CENTRE); // 设置对齐方式

contentStyle.setBorder(jxl.format.Border.ALL,

jxl.format.BorderLineStyle.THIN); // 设置边框


// 一级标题单元格样式

WritableFont wf1 = new WritableFont(WritableFont.ARIAL, 15,

WritableFont.BOLD, false); // 定义格式 字体 下划线 斜体 粗体 颜色

WritableCellFormat titleStyle1 = new WritableCellFormat(wf1); //

titleStyle1.setBackground(jxl.format.Colour.GREEN); // 设置单元格的背景颜色

titleStyle1.setAlignment(jxl.format.Alignment.CENTRE); // 设置对齐方式

titleStyle1.setBorder(jxl.format.Border.ALL,

jxl.format.BorderLineStyle.THIN); // 设置边框

// 二级标题单元格样式

WritableFont wf2 = new WritableFont(WritableFont.ARIAL, 11,

WritableFont.BOLD, false); // 定义格式 字体 下划线 斜体 粗体 颜色

WritableCellFormat titleStyle2 = new WritableCellFormat(wf2); //

titleStyle2.setBackground(jxl.format.Colour.GREY_25_PERCENT); // 设置单元格的背景颜色

titleStyle2.setAlignment(jxl.format.Alignment.CENTRE); // 设置水平对齐方式

titleStyle2

.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE); // 设置垂直对齐方式

titleStyle2.setBorder(jxl.format.Border.ALL,

jxl.format.BorderLineStyle.THIN); // 设置边框


WritableCellFormat titleStyle3 = new WritableCellFormat(wf1); //

titleStyle3.setBackground(jxl.format.Colour.BRIGHT_GREEN); // 设置单元格的背景颜色

titleStyle3.setAlignment(jxl.format.Alignment.CENTRE); // 设置对齐方式

titleStyle3.setBorder(jxl.format.Border.ALL,

jxl.format.BorderLineStyle.THIN); // 设置边框


// 设置冻结单元格

sheet.getSettings().setVerticalFreeze(2);

sheet.getSettings().setHorizontalFreeze(5);


sheet.setColumnView(0, 8); // 设置列的宽度

sheet.setColumnView(1, 10); // 设置列的宽度


sheet.setRowView(0, 0);


sheet.addCell(new Label(0, 0, "hysj5800294*#", titleStyle1));// 标题

sheet.addCell(new Label(0, 1, "出险信息 ", titleStyle2));// 标题

sheet.mergeCells(0, 2, 33, 2); // 合并单元格

sheet.addCell(new Label(0, 2, "出险信息 ", titleStyle1));// 标题


sheet.addCell(new Label(0, 3, "序号", titleStyle2));// 往单元格中填写数据

sheet.addCell(new Label(1, 3, "省内/省外", titleStyle2));// 往单元格中填写数据


if (list != null && list.size() > 0) {

for (int i = 0; i < list.size(); i++) {

sheet.setRowView(i + 3, 300); // 设置行高

//转换数据信息

//Object[] obj = (Object[]) list.get(i);

AccidentRecord ar = (AccidentRecord) list.get(i);


sheet.addCell(new Label(0, i + 4, String.valueOf(i + 1),contentStyle));// 往单元格中填写数据 序号

sheet.addCell(new Label(1, i + 4, ar.getLicenceProvince().toString(),contentStyle));//  省内/省外

}

 

}

workbook.write();// 书写到工作簿

workbook.close();// 关闭工作簿,输出完成

} catch (Exception e) {

e.printStackTrace();

}

}

}

3.功能可以实现,可以到处,点击下载确定。


4.打开已经下载的excel,如图显示结果。得到想要的结构。


让用户指定导出位置这个貌似行不通,这个要根据浏览器的来定了,ie6可以让你自己选择保存的路径,但是对于chorome 和火狐而言他们有自己默认文件保存路径

package com.mr;
 
import java.io.IOException;
import java.io.PrintWriter;
 
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
 * 利用Servlet导出Excel
 * @author CHUNBIN
 *
 */
public class ExportExcelServlet extends HttpServlet {
      
       public void doGet(HttpServletRequest request, HttpServletResponse response)
                     throws ServletException, IOException {
              doPost(request, response);
       }
 
       public void doPost(HttpServletRequest request, HttpServletResponse response)
                     throws ServletException, IOException {
              request.setCharacterEncoding("UTF-8");//设置request的编636f70797a686964616f330码方式,防止中文乱码
              String fileName ="导出数据";//设置导出的文件名称
              StringBuffer sb = new StringBuffer(request.getParameter("tableInfo"));//将表格信息放入内存
              String contentType = "application/vnd.ms-excel";//定义导出文件的格式的字符串
              String recommendedName = new String(fileName.getBytes(),"iso_8859_1");//设置文件名称的编码格式
              response.setContentType(contentType);//设置导出文件格式
              response.setHeader("Content-Disposition", "attachment; filename=" + recommendedName + "\"");//
              response.resetBuffer();
              //利用输出输入流导出文件
              ServletOutputStream sos = response.getOutputStream();
              sos.write(sb.toString().getBytes());
              sos.flush();
              sos.close();
       }
}
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http:///blog/1322097

相关阅读

关键词不能为空
极力推荐

ppt怎么做_excel表格制作_office365_word文档_365办公网