" />
乔山办公网我们一直在努力
您的位置:乔山办公网 > excel表格制作 > <em>java</em>中 讲jsp页面中传入的excel文件里的内容导入相...

<em>java</em>中 讲jsp页面中传入的excel文件里的内容导入相...

作者:乔山办公网日期:

返回目录:excel表格制作


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE> New Document </TITLE>
<META NAME="Generator" CONTENT="EditPlus">
<META NAME="Author" CONTENT="">
<META NAME="Keywords" CONTENT="">
<META NAME="Description" CONTENT="">
</HEAD>

<BODY>
<h1>通过javascript弄的,7a686964616fe58685e5aeb9365表格的样式是怎么样的,导出的excel就是怎么样的,用到了ActiveX,确保IE允许运行未标记的ActiveX</h1>
<input type="button" onclick="ExportExcel()" value="导出到excel">
<table id="table1">
<tr>
<td>asdfas<td>
<td>asdfas2<td>
<td>asdfas3<td>
</tr>
<tr>
<td>asdfas<td>
<td>asdfas2<td>
<td>asdfas3<td>
</tr>
</table>
</BODY>
<script>
function ExportExcel() {
window.clipboardData.setData("Text",document.getElementById('table1').outerHTML);
try {
var ExApp = new ActiveXObject("Excel.Application")
var ExWBk = ExApp.workbooks.add()
var ExWSh = ExWBk.worksheets(1)
ExApp.DisplayAlerts = false
ExApp.visible = true
} catch(e) {
alert("您的电脑没有安装Microsoft Excel软件!")
return false
}
ExWBk.worksheets(1).Paste;
}
</script>
</HTML>

呵呵,楼主既然思路都有了还怕写不出代码么?
你这个思路没有问题的!
可以把这个问题拆分成几个小问题,就简单多了。
第一是文件上传,可以参照Jakarta的FileUpload组件,其实也不一定要用这个,用普通的Post也就行了。
第二是Excel解析,用JSL或者POI都行
第三是数据保存,这个应该简单吧,一个循环,一行对应一条数据,写好了方法循环赋值调用就行了。
第四是查询和显示,这个更简单了,不用多说。

文件上传和Excel解析的例子网上很多的,改改就变自己的了,何必在这管别人要代码呢~
private static HSSFWorkbook readFile(String filename){
zhidaotry {
wb = new HSSFWorkbook(new FileInputStream(filename));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return wb;
}
两种jar包 jxl 和 apache的集成开源 写入文件或者直接保存

一、列表页面的部分代码
[html] view plaincopy
<input type="button" class="cxBut2" value="导出数据" onclick="_export()"/>
[javascript] view plaincopy
function _export(){
document.location.href="download.jsp?czId=<%=czId%>&czNum=<%=czNum%>&beginIssue=<%=beginIssue%>&endIssue=<%=endIssue%>&pageNum=<%=pageNum%>&czName="+$("#czId").find("option:selected").text();
}

二、download.jsp
[html] view plaincopy
<%@ page contentType="application/vnd.ms-excel" language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@page import="com.zhcw.kaijiang.service.ExportExcal"%>
<%@page import="java.io.OutputStream"%>
<%
String czNum = request.getParameter("czNum");
String czId = request.getParameter("czId");
String czName = request.getParameter("czName");
String beginIssue = request.getParameter("beginIssue");
String endIssue = request.getParameter("endIssue");
String pageNum = request.getParameter("pageNum");
response.resetBuffer();
response.setHeader("Content-Disposition", "attachment;filename="+ new String(czName.getBytes("UTF-8"), "iso8859-1")+".xls");//指定下载的文件名
response.setContentType("application/vnd.ms-excel");
try{
ExportExcal exportExcal = new ExportExcal(beginIssue, czId, czName, czNum, endIssue, pageNum);
exportExcal.export(response.getOutputStream());
}catch(Exception ex){
ex.printStackTrace();
}
%>

3.这个页面主要负责接收上一个页面传来的数据,e68a84e799bee5baa6e997aee7ad94335然后调用后台相应的导出方法来实现导出功能。
注意:这两行代码必须要写:

[html] view plaincopy
response.resetBuffer();<pre name="code" class="html">response.setContentType("application/vnd.ms-excel");</pre>
<pre></pre>
<p></p>
<pre></pre>
<p></p>
<p></p>
三、后台处理类<pre name="code" class="java">package com.zhcw.kaijiang.service;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.text.DecimalFormat;
import java.util.List;

import jxl.Workbook;
import jxl.format.Alignment;
import jxl.format.Border;
import jxl.format.BorderLineStyle;
import jxl.write.Label;
import jxl.write.WritableCellFormat;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;

import com.common.PropertiesOperator;
import com.crawler.entity.KaiJiangInfo;
import com.zhcw.kaijiang.util.StringUtil;

/**
* 将查询出来的信息导出到excel中
*
* @author zhcw
*
*/
public class ExportExcal {
private String czNum = "";
private String czId = "";
private String czName = "";
private String beginIssue = "";
private String endIssue = "";
private String pageNum = "";

public ExportExcal(String beginIssue, String czId, String czName, String czNum, String endIssue, String pageNum) {
super();
this.beginIssue = (beginIssue == null ? "" : beginIssue);
this.endIssue = (endIssue == null ? "" : endIssue);
this.czId = (czId == null ? "" : czId);
this.czName = (czName == null ? "" : czName);
this.czNum = (czNum == null || czNum.trim().equals("") || czNum.trim().equals("null")) ? "30" : czNum;
this.pageNum = (pageNum == null || pageNum.trim().equals("") || pageNum.trim().equals("null")) ? "1" : pageNum;
}

public List<KaiJiangInfo> kaiJiangInfoList() {
KaijiangInfoService kaijiangInfoService = new KaijiangInfoService();
if (this.beginIssue!=null && this.beginIssue.trim().length() > 0) {
return kaijiangInfoService.getSplitPageByCzIdNew(Long.valueOf(this.czId), Integer.valueOf(pageNum), 400, this.beginIssue, this.endIssue);
} else {
return kaijiangInfoService.getSplitPageByCzIdNew(Long.valueOf(this.czId), Integer.valueOf(pageNum), Integer.parseInt(czNum));
}
}

public void export(OutputStream output) throws Exception {
PropertiesOperator propertiesOperator = new PropertiesOperator();
String cz_id_css_1 = propertiesOperator.getMessage("cz_id_css_1") == null ? "" : propertiesOperator.getMessage("cz_id_css_1").trim();
String cz_id_css_2 = propertiesOperator.getMessage("cz_id_css_2") == null ? "" : propertiesOperator.getMessage("cz_id_css_2").trim();
String cz_id_css_3 = propertiesOperator.getMessage("cz_id_css_3") == null ? "" : propertiesOperator.getMessage("cz_id_css_3").trim();
String cz_id_css_4 = propertiesOperator.getMessage("cz_id_css_4") == null ? "" : propertiesOperator.getMessage("cz_id_css_4").trim();
String cz_id_css_5 = propertiesOperator.getMessage("cz_id_css_5") == null ? "" : propertiesOperator.getMessage("cz_id_css_5").trim();
if (StringUtil.contains(this.czId, cz_id_css_1)) {
this.exportSSQExcel(output);
} else if (StringUtil.contains(this.czId, cz_id_css_2)) {
this.export3DExcel(output);
}
}
private void exportExcel(String type,OutputStream output) throws Exception {
WritableWorkbook workbook = Workbook.createWorkbook(output);// 创建工作薄
WritableSheet sheet = workbook.createSheet(this.czName, 0);// 创建第一个工作表,name:工作表名称
//设置列宽度
sheet.setColumnView(0,7);
sheet.setColumnView(1,15);
sheet.setColumnView(2,13);
sheet.setColumnView(3,25);
sheet.setColumnView(4,15);
sheet.setColumnView(6,15);
sheet.setColumnView(8,15);
sheet.setColumnView(10,15);
sheet.setColumnView(11,15);
WritableCellFormat format = new WritableCellFormat();
format.setAlignment(Alignment.CENTRE);
format.setBorder(Border.ALL, BorderLineStyle.THIN);
int row = 0;
//合并标题行
sheet.mergeCells(0, row, 11, row);

// 合并
sheet.mergeCells(0, row + 1, 0, row + 2);
sheet.mergeCells(1, row + 1, 1, row + 2);
sheet.mergeCells(2, row + 1, 2, row + 2);
sheet.mergeCells(3, row + 1, 3, row + 2);
sheet.mergeCells(4, row + 1, 4, row + 2);
sheet.mergeCells(11, row + 1, 11, row + 2);

sheet.mergeCells(5, row + 1, 6, row + 1);
sheet.mergeCells(7, row + 1, 8, row + 1);
sheet.mergeCells(9, row + 1, 10, row + 1);

Label label = null;// 用于写入文本内容到工作表中去
// 开始写入第一行,即标题栏
if(this.beginIssue.length()>0){
label = new Label(0, row, czName+"从"+this.beginIssue+" 到 "+this.endIssue+"期的中奖信息",format);// 参数依次代表列数、行数、内容
sheet.addCell(label);// 写入单元格
}else{
label = new Label(0, row, czName+"的前 "+this.czNum+" 期的中奖信息",format);// 参数依次代表列数、行数、内容
sheet.addCell(label);// 写入单元格
}
//第二行写表头
label = new Label(10, num, df.format(kaiJiangInfo.getBonusThree()),format);// 参数依次代表列数、行数、内容
sheet.addCell(label);// 写入单元格
//只有双色球有奖池金额
if(type!=null && type.trim().equals("ssq")){
label = new Label(11, num, df.format(kaiJiangInfo.getBonusPool()),format);// 参数依次代表列数、行数、内容
sheet.addCell(label);// 写入单元格
}
}
workbook.write();
workbook.close();
}
private void exportSSQExcel(OutputStream output) throws Exception {
this.exportExcel("ssq", output);
}

private void export3DExcel(OutputStream output) throws Exception {
this.exportExcel("3d", output);
}
}
</pre>


<div style="padding-top:20px">
</div>

相关阅读

关键词不能为空

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