============前提加入jar包jxl.jar=========================

// 从数据库导出数据到excel
 public List<Xskh> outPutExcel() {

String url= request.getSession().getServletContext()
     .getRealPath("/")
     + "upload/"; // 创建文件

File dirPath = new File(ctxPath);
   if (!dirPath.exists()) {
    dirPath.mkdir();// 创建文件夹
   }

List<Xskh> list = excelDao.getInfo(" select x from Xskh  ");// 查询数据库导出资料
  try {

WritableSheet sheet = null;
   // 创建excel文件
   WritableWorkbook book = Workbook.createWorkbook(new File(url+"data.xls"));
   // 生成名为“第一页”的工作表,参数0表示这是第一页
   sheet = book.createSheet(" 第一页 ", 0);

// 设置行宽、高
   sheet.getSettings().setDefaultColumnWidth(20);
   sheet.getSettings().setDefaultRowHeight(480);

// 设置字体
   WritableFont font = new WritableFont(WritableFont.ARIAL, 12,
     WritableFont.BOLD, false, UnderlineStyle.NO_UNDERLINE,
     Colour.RED);
   WritableCellFormat wcfF = new WritableCellFormat(font);

// 字体居中
   wcfF.setVerticalAlignment(VerticalAlignment.CENTRE);
   // 自动换行
   wcfF.setWrap(true);

// 在Label对象的构造子中指名单元格位置是第一列第一行(0,0)
   // 以及单元格内容为test
   Label label = new Label(0, 0, "简称 ", wcfF);
   // 将定义好的单元格添加到工作表中
   sheet.addCell(label);
   Label labelcomplay = new Label(1, 0, "公司", wcfF);
   sheet.addCell(labelcomplay);

Label labeltel = new Label(2, 0, "固定电话", wcfF);
   sheet.addCell(labeltel);

Label labelmobile = new Label(3, 0, "移动电话", wcfF);
   sheet.addCell(labelmobile);

Label labelfax = new Label(4, 0, "传真", wcfF);
   sheet.addCell(labelfax);
   Label labeladdr = new Label(5, 0, "地址", wcfF);
   sheet.addCell(labeladdr);
   Label labelsite = new Label(6, 0, "网址", wcfF);
   sheet.addCell(labelsite);

Label labelmail = new Label(7, 0, "邮箱", wcfF);
   sheet.addCell(labelmail);
   Label qq = new Label(8, 0, "qq", wcfF);
   sheet.addCell(qq);
   Label people = new Label(9, 0, "联系人", wcfF);
   sheet.addCell(people);

Label linkbm = new Label(10, 0, "联系人部门", wcfF);
   sheet.addCell(linkbm);
   Label linkejob = new Label(11, 0, "联系人职务", wcfF);
   sheet.addCell(linkejob);
   Label linkphone = new Label(12, 0, "联系人电话", wcfF);
   sheet.addCell(linkphone);

// 所有资料
   for (int i = 0; i < list.size(); i++) {

Label labelad = new Label(0, i + 1, list.get(i).getName());
    sheet.addCell(labelad);
    Label labelmail2 = new Label(1, i + 1, list.get(i).getCompany());
    sheet.addCell(labelmail2);

Label tel = new Label(2, i + 1, list.get(i).getTelephone());
    sheet.addCell(tel);
    Label mobile = new Label(3, i + 1, list.get(i).getMobile());
    sheet.addCell(mobile);
    Label fax = new Label(4, i + 1, list.get(i).getFax());
    sheet.addCell(fax);
    Label addr = new Label(5, i + 1, list.get(i).getAddress());
    sheet.addCell(addr);
    Label site = new Label(6, i + 1, list.get(i).getWww());
    sheet.addCell(site);
    Label mail = new Label(7, i + 1, list.get(i).getEmail());
    sheet.addCell(mail);
    Label q = new Label(8, i + 1, list.get(i).getQq());
    sheet.addCell(q);
    Label peo = new Label(9, i + 1, list.get(i).getLikeme());
    sheet.addCell(peo);
    Label bm = new Label(10, i + 1, list.get(i).getKfPeoBm());
    sheet.addCell(bm);
    Label zw = new Label(11, i + 1, list.get(i).getKfPeoZhiwei());
    sheet.addCell(zw);
    Label linktel = new Label(12, i + 1, list.get(i).getKfPeoTel());
    sheet.addCell(linktel);//创建excel

}

// 写入数据并关闭文件
   book.write();
   book.close();

  String result = download(request, response, url+ "/data.xls", "data.xls");//下载的方法
   if ("unexists".equals(result)) {
    request.setAttribute("msg", "抱歉,文件不存在,请重新导入文件!");
   } else if ("error".equals(result)) {
    request.setAttribute("msg", "抱歉,当前服务器用户过多,请稍后再试!");
   } else {
    request.setAttribute("msg", "数据导出成功!");

}

} catch (Exception e) {
   e.printStackTrace();
   return null;

}
  return list;
 }

---------------------下载的方法------------------

public  String download(HttpServletRequest request,
   HttpServletResponse response, String urlandfile, String fileName)
   throws Exception {
  String msg = null;
  try {
   response.setCharacterEncoding("UTF-8");
   response.setContentType("text/html");
   javax.servlet.ServletOutputStream ou = response.getOutputStream();
   // 路径
   java.io.File file = new java.io.File(urlandfile);

if (!file.exists()) {
    System.out.println(file.getAbsolutePath() + " 文件不能存在!");
    msg = "unexists";
    return msg;
   }

// 读取文件流
   java.io.FileInputStream fileInputStream = new java.io.FileInputStream(
     file);

// 下载文件
   // 设置响应头和下载保存的文件名

response.setContentType("application/x-msdownload");// 弹出下载的框

response.setContentLength((int) file.length());// 下载统计文件大小的进度
   response.setHeader("Content-Disposition", "attachment; filename="
     + fileName);
   // 下载框的信息
   if (fileInputStream != null) {
    int filelen = fileInputStream.available();
    // 文件太大时内存不能一次读出,要循环

byte a[] = new byte[filelen];

fileInputStream.read(a);

ou.write(a);
   }
   fileInputStream.close();

ou.close();

msg = "success";
  } catch (Exception e) {
   e.printStackTrace();
   msg = "error";

}
  // 解决完成后使用一切正常,但是总抛出java.lang.IllegalStateException异常主要是流还存在
  return msg;
 }

java 对excel操作 读取、写入、修改数据;导出数据库数据到excel的更多相关文章

  1. 在ASP.NET中将GridView数据导出到Word、Excel

    在ASP.NET中将GridView数据导出到Word.Excel asp.net,导出gridview数据到Word,Excel,PDF   #region Export to Word, Exce ...

  2. python openpyxl模块实现excel的读取,新表创建及原数据表追加新数据

    当实际工作需要把excel表的数据读取出来,或者把一些统计数据写入excel表中时,一个设计丰富,文档便于寻找的模块就会显得特别的有吸引力,本文对openpyxl模块的一些常见用法做一些记录,方便工作 ...

  3. java导出数据到excel里:直接导出和导出数据库数据

    一.直接导出 package com.ij34.util; import java.io.FileNotFoundException; import java.io.FileOutputStream; ...

  4. Java注解(Annotation)用法:利用注解和反射机制指定列名导出数据库数据

    闲来没事,想了一个应用的例子:用java如何把数据库的数据根据我们指定的某几列,如第2列,第4列,第6列导出来到Excel里? 写代码也是为了应用的,写好的代码更重要的是在于思考.我自己思考了这个示例 ...

  5. 配置ODBC DSN数据源,导出数据库数据到Excel过程记录

    一.前言 工作中我们可能遇到这样的需要:查询数据库中的信息,并将结果导出到Excel文件.这本来没什么,但数据量比较大时,用PLSQL.toad导出Excel会出现内存不足等情况,使用odbc+Mic ...

  6. 2019.06.05 ABAP EXCEL 操作类代码 OLE方式(模板下载,excel上传,内表下载)

    一般使用标准的excel导入方法9999行,修改了标准的excel导入FM 整合出类:excel的 模板下载,excel上传,ALV内表下载功能. 在项目一开始可以SE24创建一个类来供整体开发使用, ...

  7. 导出数据库数据到Excel表

    后台需要将用户信息数据导入到Excel表中提供给相关人员: 首先查询数据就不多说了: 导入Excel表直接亮代码(采用的是jxl的jar包提供的方法): public static File Impo ...

  8. BCP 导入导出数据库数据

    使用 bcp 将数据库迁移到 Azure SQL Database --所有 都是在本机sql上运行--先开启cmdshellEXEC sp_configure 'show advanced opti ...

  9. 接口json数据与数据库数据循环比对校验

    创建测试计划,加载数据库驱动: 线程组: csv配置元件: 注:Filename用的是相对路径,csv文件要与jmeter脚本文件在同一目录 JDBC连接配置: jdbc请求: 用户定义的变量: ht ...

随机推荐

  1. 物联网操作系统HelloX已成功移植到MinnowBoard MAX开发板上

    在HelloX开发团队的努力下,以及Winzent Tech公司(总部在瑞典斯德哥尔摩)的支持下,HelloX最新版本V1.78已成功移植到MinnowBoard MAX开发板上.相关源代码已经发布到 ...

  2. erl0009 - erlang 读取时间瓶颈解决办法

    读取时间erlang提供有两种方式: 1.erlang:now(); 2.os:timestamp(); 以上两种方式由于erlang系统需要保证读取精度,当并发读取的时候会引起加锁.系统频繁读取时间 ...

  3. 【C#学习笔记】文本复制到粘贴板

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...

  4. switchover步骤切换

    主库 alter system switch logfile; alter system set log_archive_dest_state_2='defer'; select switchover ...

  5. 【转】eclipse怎么设置字体大小

    原文网址:http://jingyan.baidu.com/article/f96699bb9442f3894e3c1b15.html 1. 打开eclipse,找到window 2.  点击后在下拉 ...

  6. 【转】搭建Python的Eclipse开发环境之安装PyDev插件--离线安装

    原文网址:http://blog.csdn.net/wangpingfang/article/details/7181223 使用update site安装pydev插件 注意:该安装指南针对ecli ...

  7. OpenGL学习之路(一)

    1 引子 虽然是计算机科班出身,但从小对几何方面的东西就不太感冒,空间想象能力也较差,所以从本科到研究生,基本没接触过<计算机图形学>.为什么说基本没学过呢?因为好奇(尤其是惊叹于三维游戏 ...

  8. suse下设置IP的两种方法

    /Files/yzhxhwt/DB_51aspx.rar 第一种SUSE Linux IP设置方法ifconfig eth0 192.168.1.22 netmask 255.255.255.0 up ...

  9. check windows return character

    #ifndef _FILE_CHECK_H#define _FILE_CHECK_H#include <string.h>#include <vector> const int ...

  10. [C#搜片神器] 之P2P中DHT网络爬虫原理

    继续接着上一篇写:使用C#实现DHT磁力搜索的BT种子后端管理程序+数据库设计(开源)[搜片神器] 昨天由于开源的时候没有注意运行环境,直接没有考虑下载BT种子文件时生成子文件夹,可能导致有的朋友运行 ...