ss2h实现Excel下载

/**
  * 导出Excel
  *
  * @return
  * @throws Exception
  */
 public String export() throws Exception {
  String sid = request.getParameter("sid");
  response.setCharacterEncoding("utf-8");
  response.setContentType("utf-8");
  response.setContentType("application/vnd.ms-excel");
  String fileName = "问卷调查发送记录表";// 设置excel文件的名字
  if (!fileName.equals("")) {
   String header = "attachment; filename="
     + new String((fileName + ".xls").getBytes(), "iso8859-1");
   response.setHeader("Content-Disposition", header);
  }

  // 创建响应输出流对象
  OutputStream out = response.getOutputStream();
  SmssendRcorderDetail send = null;
  QuestionnaireBaseInformation info = null;
  List gList = iquery.querySmssendRcorderDetailById(Long.valueOf(sid));
  List list = iquery.QueryQuesionnaireById(sid);
  if (list != null && list.size() > 0) {
   info = (QuestionnaireBaseInformation) list.get(0);
  }
  if (gList == null) {
   return null;
  }
  try {
   // 创建Excls文件
   WritableWorkbook workbook = Workbook.createWorkbook(out);
   // 创建工作簿(名称,索引)
   WritableSheet ws = workbook.createSheet("问卷调查发送记录表", 0);

   /*-------------------设置标题---------------------*/
   // 设置字体
   WritableFont font = new WritableFont(WritableFont.ARIAL, 11,
     WritableFont.BOLD,// 加粗显示
     false, UnderlineStyle.NO_UNDERLINE, // 设置下划线
     jxl.format.Colour.RED);
   // 设置单元格格式化对象
   WritableCellFormat cellFormat = new WritableCellFormat(font);
   // cellFormat.setAlignment(Alignment.CENTRE);// 显示字体居中
   // cellFormat.setVerticalAlignment(VerticalAlignment.CENTRE);//
   // 设置垂直居中

   /*---------------设置表头---------------*/
   // excel显示的表头
   String[] titles = new String[] { "手机号", "姓名", "电表号", "短信发送时间" };
   WritableFont fontTitle = new WritableFont(WritableFont.ARIAL, 11,
     WritableFont.NO_BOLD,// 不加粗显示
     false, UnderlineStyle.NO_UNDERLINE, Colour.BLACK);
   WritableCellFormat cellFormat_title = new WritableCellFormat(
     fontTitle);// 设置样式
   cellFormat_title.setAlignment(Alignment.CENTRE);
   // 设置单元格背景色
   cellFormat_title.setBackground(Colour.GREY_40_PERCENT);
   for (int i = 0; i < titles.length; i++) {
    // 参数: 列,行,内容,格式
    Label title = new Label(i, 1, "" + titles[i], cellFormat_title);

    ws.addCell(title);
   }

   /*----------------导出数据生成Excel---------------*/
   WritableCellFormat cellFormat_data = new WritableCellFormat();// 设置样式
   cellFormat_data.setAlignment(Alignment.CENTRE);
   cellFormat_data.setBorder(Border.ALL, BorderLineStyle.THIN);
   // 将要生成的列加载在excel中
   String qtitle = "";
   String qanswer = "";
   for (int i = 0; i < gList.size(); i++) {
    send = (SmssendRcorderDetail) gList.get(i);
    ws.mergeCells(0, 0, 6, 0);// 合并单元格,其中的3为显示的行数减1
    ws.mergeCells(0, 1, 6, 1);// 合并单元格,其中的3为显示的行数减1
    qtitle = "问卷标题:" + send.getInvestigationTitle();// excel文件的头
    if (info.getInvestigationType().equals("0")) {
     qanswer = "调查问题与答案:【" + "单选】"
       + send.getInvestigationProblem()
       + send.getInvestigationAnswer();
    } else {
     qanswer = "调查问题与答案:【" + "自由回答】"
       + send.getInvestigationProblem()
       + send.getInvestigationAnswer();
    }

    Label motifs = new Label(0, 0, qtitle, cellFormat);
    Label qmotifs = new Label(0, 1, qanswer, cellFormat);
    ws.addCell(motifs);// 写入主题
    ws.addCell(qmotifs);

    Label phone = new Label(0, i + 2, send.getUserPhoneNo(),
      cellFormat_data);

    Label name = new Label(1, i + 2, send.getUserName(),
      cellFormat_data);

    Label syns = new Label(2, i + 2, send.getSynstatus(),
      cellFormat_data);
    Label time = new Label(3, i + 2, send.getSmsSendtime(),
      cellFormat_data);

    // 设置列宽
    ws.setColumnView(0, 16); // 设置列宽,第1列
    ws.setColumnView(3, 20);// 设置列宽,第4列

    // 邦定要生成的字段
    ws.addCell(phone);
    ws.addCell(name);
    ws.addCell(syns);
    ws.addCell(time);

   }
   // 写成文件
   workbook.write();
   workbook.close();

  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (WriteException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  out.close();
  response.flushBuffer();

  return null;
 }

 

<a  href="<%=path%>/questionnaire/questionAction!export.action?sid=<s:property value='#question.businessNo' />">输出Excel</a>

要在Java中实现Excel文件的下载,你可以使用Apache POI库来生成Excel文件,并使用Java Servlet或Spring MVC来处理下载请求。以下是一个简单的示例代码: ```java import org.apache.poi.ss.usermodel.*; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import javax.servlet.http.HttpServletResponse; import java.io.IOException; public class ExcelDownloader { public static void downloadExcel(HttpServletResponse response) throws IOException { // 创建工作簿 Workbook workbook = new XSSFWorkbook(); // 创建工作表 Sheet sheet = workbook.createSheet("Sheet1"); // 创建行 Row headerRow = sheet.createRow(0); // 创建单元格 Cell headerCell1 = headerRow.createCell(0); headerCell1.setCellValue("姓名"); Cell headerCell2 = headerRow.createCell(1); headerCell2.setCellValue("年龄"); // 创建数据行 Row dataRow = sheet.createRow(1); Cell dataCell1 = dataRow.createCell(0); dataCell1.setCellValue("张三"); Cell dataCell2 = dataRow.createCell(1); dataCell2.setCellValue(25); // 设置响应头 response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"); response.setHeader("Content-Disposition", "attachment; filename=example.xlsx"); // 写入响应流 workbook.write(response.getOutputStream()); workbook.close(); } } ``` 在你的Servlet或Controller中,调用`ExcelDownloader.downloadExcel(response)`方法来触发下载: ```java import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; public class MyServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { ExcelDownloader.downloadExcel(response); } } ``` 这样,当你访问对应的Servlet或Controller时,就会自动下载名为"example.xlsx"的Excel文件。记得将代码中的内容替换为你实际需要的数据和文件名。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值