您的位置 首页 java

java中文件导出Excel的公共方法

  • 文件导出Excel 的时候,比较繁琐,如果有大量的文件导出,必然出现重复代码,这样我们就需要一个公共方法。

  • 下面写的一个工具类:

    package com.teamnavi.zstrade.common.util;

  • public class ImportExcleUtil {

  • public void ImportExcel(HttpServletResponse response,String name){}

  • //设置需要的各种字体。

  • /**

  • *

  • * @param size 字体大小

  • * @param bold 字体是否加粗 1 加粗 0 不加粗

  • * @param leftAnd RIGHT 1 居中 2 靠左 3 靠右

  • * @param Border 边框 1 all 2 left 3 right 4 top 5 botton 6 none

  • * @return

  • * @throws WriteException

  • */

  • public static WritableCellFormat getWritableCellFormat(int size,int bold, int leftAndRight,int border)

  • throws WriteException{

  • WritableFont wf=null;

  • if(1==bold){

  • wf = new WritableFont(WritableFont.createFont(” 微软雅黑 “), size, WritableFont.BOLD, false );

  • }else

  • {

  • wf = new WritableFont(WritableFont.createFont(“微软雅黑”), size, WritableFont.NO_BOLD, false);

  • }

  • WritableCellFormat wcfF = new WritableCellFormat(wf);

  • wcfF.setVerticalAlignment(VerticalAlignment.CENTRE);//设置上下对齐

  • //设置左右对齐方式。

  • if(1==leftAndRight){

  • wcfF.setAlignment(Alignment.CENTRE);

  • } if(2==leftAndRight){

  • wcfF.setAlignment(Alignment.LEFT);

  • } if(3==leftAndRight){

  • wcfF.setAlignment(Alignment.RIGHT);

  • }

  • //设置边框

  • if(1== border ){

  • wcfF.setBorder(jxl.format.Border.ALL, jxl.format.BorderLineStyle.THIN);

  • }if(2==border){

  • wcfF.setBorder(jxl.format.Border.LEFT, jxl.format.BorderLineStyle.THIN);

  • }

  • if(3==border){

  • wcfF.setBorder(jxl.format.Border.RIGHT, jxl.format.BorderLineStyle.THIN);

  • }

  • if(4==border){

  • wcfF.setBorder(jxl.format.Border.TOP, jxl.format.BorderLineStyle.THIN);

  • }

  • if(5==border){

  • wcfF.setBorder(jxl.format.Border.BOTTOM, jxl.format.BorderLineStyle.THIN);

  • }

  • if(6==border){

  • wcfF.setBorder(jxl.format.Border.NONE, jxl.format.BorderLineStyle.THIN);

  • }

  • return wcfF;

  • }

  • /**

  • *

  • * @param heads 表头名字数组

  • * @param sheet

  • * @param wcfF12 字体

  • * @param line 列

  • * @param row 行

  • * @throws Exception

  • */

  • public static void getHeadForm(String[] heads,WritableSheet sheet,WritableCellFormat wcfF12,int line, int row ) throws Exception

  • {

  • if(null!=heads && heads.length>0){

  • for (int i = 0; i < heads.length; i++) {

  • sheet.addCell(new jxl.write.Label(line+i, row, heads[i], wcfF12));

  • }

  • }

  • }

  • /**

  • *

  • * @param response

  • * @param name 文件名称

  • * @return

  • */

  • public static List getExcleCharSet(HttpServletResponse response,String name){

  • response.reset();

  • response.setCharacterEncoding(“UTF-8”);// 设置字符集

  • WritableWorkbook book=null;

  • OutputStream os=null;

  • List<Object> list=new ArrayList<Object>();

  • // 设置带有字形formatting的对象标题字体

  • // 创建工作流

  • try {

  • // 设置弹出对话框

  • response.setContentType(“application/DOWLOAD”);

  • response.setCharacterEncoding(“UTF-8”);

  • // 设置生成的文件名字

  • response.addHeader(“Content-Disposition”, “inline; filename=”

  • + new String(name.getBytes(“GB2312”), “ISO8859_1”)

  • + “.xls”);

  • os = response.getOutputStream();

  • // 初始化工作表

  • book = Workbook.createWorkbook(os);

  • list.add(os);

  • list.add(book);

  • return list;

  • } catch (IOException e1) {

  • // logger.error(“导出excel出现IO异常”, e1);

  • throw new ServiceException(“导出失败”, e1);

  • }

  • }

  • }

然后再需要用的地方调用这三个class就可以了:

下面是service 层调用:

  • if (null != resultWrapper) {

  • Vector<User_order_ckUpDTO> resultUser_order_ckDTOList = (Vector<User_order_ckUpDTO>) resultWrapper

  • .getObject();

  • String name = “出库单”;

  • response.reset();

  • response.setCharacterEncoding(“UTF-8”);// 设置字符集

  • // 设置带有字形formatting的对象标题字体

  • // WritableFont.BOLD, true);

  • // 创建工作流

  • List<Object> list= ImportExcleUtil.getExcleCharSet(response, name);

  • OutputStream os = (OutputStream) list.get(0) ;

  • WritableWorkbook book = (WritableWorkbook) list.get(1) ;

  • try {

  • // 设置日期格式

  • // SimpleDateFormat sf = new SimpleDateFormat(“yyyy-MM-dd”);//

  • // 日期格式1

  • // SimpleDateFormat sf2 = new

  • // SimpleDateFormat(“yyyy-MM-dd HH:mm:ss”);// 日期格式1

  • WritableSheet sheet = book.createSheet(name, 0);// 设置sheet名称

  • sheet.getSettings().setAutomaticFormulaCalculation(true);

  • // 设置默认列宽

  • sheet.getSettings().setDefaultColumnWidth(11);

  • // 设置默认行高

  • // 表字段名

  • // sheet.mergeCells(0, 0, 11, 0);// 1-k合并

  • // sheet.setRowView(0, 800); // 设置第1行行高

  • //

  • // // sheet.setc

  • // sheet.mergeCells(0, 1, 11, 1);// 2-l合并

  • // sheet.mergeCells(0, 2, 11, 2);// 2-l合并

  • String[] heads = { “单据编码”, “单据类型”, “单据状态”, “仓库名称”, “出库时间”,”作废状态”};

  • WritableCellFormat wcfF12 = ImportExcleUtil

  • .getWritableCellFormat(12, 1, 1, 1);

  • ImportExcleUtil.getHeadForm(heads, sheet, wcfF12, 0, 0);// 得到开头列表

  • WritableCellFormat wcfF11 = ImportExcleUtil

  • .getWritableCellFormat(10, 0, 1, 1);

  • // 将数据追加

  • if (resultUser_order_ckDTOList != null

  • && resultUser_order_ckDTOList.size() > 0) {

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

  • User_order_ckUpDTO p = resultUser_order_ckDTOList

  • .get(i);

  • sheet.addCell(new jxl.write.Label(0, 1 + i, p

  • .getOrder_number(), wcfF11));

  • sheet.addCell(new jxl.write.Label(1, 1 + i, p

  • .getOrder_type(), wcfF11));

  • sheet.addCell(new jxl.write.Label(2, 1 + i, p

  • .getOrder_status(), wcfF11));

  • sheet.addCell(new jxl.write.Label(3, 1 + i, p

  • .getStore_house_name(), wcfF11));

  • sheet.addCell(new jxl.write.Label(4, 1 + i, p

  • .getCk_date(), wcfF11));

  • sheet.addCell(new jxl.write.Label(5, 1 + i, p

  • .getIs_forbid(), wcfF11));

  • }

  • }

  • book.write();

  • book. close ();

  • } catch (Exception e) {

  • throw new ServiceException(“导出失败”, e);

  • } finally {

  • if (null != os) {

  • try {

  • os.flush();

  • os.close();

  • } catch (IOException e) {

  • // logger.error(“关流出现异常”, e);

  • e.printStackTrace();

  • }

  • }

  • }

  • }

谢谢各位读者 评论 指正错误,谢谢。

文章来源:智云一二三科技

文章标题:java中文件导出Excel的公共方法

文章地址:https://www.zhihuclub.com/180229.shtml

关于作者: 智云科技

热门文章

网站地图