1.jsp
<script>
$(document).ready( function(){
$("#btnup").click(function(){
dialog = $.ydialog.create();
if(null!=dialog){
dialog.buttons = {
"开始上传":function(){
var data = dialog.iframe().upload();
dialog.button('开始上传').disable();
},
"关闭":function(){
dialog.close();
}
};
dialog.url = 'InImport.action';
dialog.show('导入信息',500,500);
}
return false;
});
});
</script>
<table id="tb1">
<tr ><td id="eventd">页面</td>
<td id="eventd"><input id="btnup" type="button" value="上传文件"/></td>
</tr>
</table>
2.jsp
<script type="text/javascript">
function upload(){
filename= $('#inFile').val();
if(filename==""){
alert("请选择要上传的数据文件!");
return;
}
alert("本次操作的时间可能较长,请耐心等待!");
var url= 'ataImport.action';
$('form')[0].action=url;
$('form')[0].submit();
}
</script>
</head>
<body>
<s:form method="post" enctype="multipart/form-data">
<div style="width: 100%;">
<table cellspacing="0" cellpadding="0" class="resultTable tablestyle4">
<tr>
<td class="tdalignR">数据文件:</td>
<td>
<s:file id="inFile" name="inFile" style="width:260px;"></s:file>
</td>
</tr>
<tr>
<td colspan="2">上传EXCEL格式需如图所示:</td>
</tr>
<tr>
<td colspan="2"><img alt="" style=" margin:5px;" src="hua.png" width="450" height="160">
</td>
</tr>
</table>
</div>
</s:form>
3.action:
package com.bytsp.third.action;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import com.bytsp.common.action.BaseAction;
public class CommonAction extends BaseAction{
/**
*
*/
private static final long serialVersionUID = -6933312854727410976L;
private HttpServletRequest request;
private HttpServletResponse response;
private File inFile;// 上传的文件
private String inFileFileName; // 原文件名
private static final String FILE_LINK="/js/data.js";
public String linkMain(){
return "linkpage";
}
public String enterInImport(){
return "enterInImport";
}
public String dataImport() throws IOException{
request = request();
response = response();
response.setContentType("text/plain;charset=utf-8");
response.setHeader("Cache-Control", "no-cache");
try{
Workbook wb = this.creatWorkBook(this.inFileFileName, new FileInputStream(this.inFile));
Sheet sheet = wb.getSheetAt(0);
String sheetName = sheet.getSheetName();
Row titleRow = sheet.getRow(0);
Iterator<Cell> iCell = titleRow.iterator();
// 保存列名
List<String> lstCellName = new ArrayList<String>();
while (iCell.hasNext()) {
lstCellName.add(iCell.next().getStringCellValue());
}
String name = "";
String type = "";
String price = "";
String remark = "";
StringBuffer buff = new StringBuffer();
buff.append("var priceData = {");
buff.append("\n");
buff.append("'title':"+"'"+sheetName+"'");
buff.append(",\n");
buff.append("'data':[");
buff.append("{'name':'"+lstCellName.get(0)+"'");
buff.append(",'type':'"+lstCellName.get(1)+"'");
buff.append(",'price':'"+lstCellName.get(2)+"'");
buff.append(",'remark':'"+lstCellName.get(3)+"'}");
buff.append("\n");
for (int i = 1; i <= sheet.getLastRowNum(); i++){
Row row = sheet.getRow(i);
name = this.cellValue(row, 0);
type = this.cellValue(row, 1);
price = this.cellValue(row, 2);
remark =this.cellValue(row, 3);
buff.append(",{'name':"+"'"+name+"'");
buff.append(",'type':"+"'"+type+"'");
buff.append(",'price':"+"'"+price+"'");
buff.append(",'remark':"+"'"+remark+"'");
buff.append("}\n");
}
buff.append("]");
buff.append("\n");
buff.append("};");
byte[] b = buff.toString().getBytes("UTF-8");
String path = request.getSession().getServletContext().getRealPath(FILE_LINK);
File file = new File(path);
if(!file.exists()){
file.createNewFile();
}
FileOutputStream out = new FileOutputStream(file);
out.write(b, 0, b.length);
out.flush();
out.close();
response.getWriter().print(toReturnMsg("提示", "导入成功!"));
}catch(Exception e){
response.getWriter().print(toReturnMsg("错误提示", e.getMessage()));
return null;
}
return null;
}
/**
* 读取单元格数据
* @author yf 2012-4-25
* */
private String cellValue(Row row,int cellIndex){
String value = "";
Cell cell = row.getCell(cellIndex);
if(cell!=null){
if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC) {
value = String.valueOf(cell.getNumericCellValue());
} else {
value = cell.getStringCellValue();
}
if(value==null){
value = "";
}
}
return value;
}
/**
* @return the inFile
*/
public File getInFile() {
return inFile;
}
/**
* @param inFile the inFile to set
*/
public void setInFile(File inFile) {
this.inFile = inFile;
}
/**
* @return the inFileFileName
*/
public String getInFileFileName() {
return inFileFileName;
}
/**
* @param inFileFileName the inFileFileName to set
*/
public void setInFileFileName(String inFileFileName) {
this.inFileFileName = inFileFileName;
}
}
4.显示的jsp页面
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta name="viewport" content="user-scalable=no, width=device-width, initial-scale=1.0, maximum-scale=1.0">
<title>产品报价</title>
<script type="text/javascript" src="../../js/jquery/jquery-1.4.4.min.js"></script>
<script type="text/javascript" src="js/data.js"></script>
<script language="javascript">
var dialog = null;
var title = '';
var data = '';
var table = '';
$(document).ready(function() {
title = priceData.title;
data = priceData.data;
for(i in data){
var obj = data[i];
if(i==0){
table = table + '<tr><th>'+obj.name+'</th><th>'+obj.type+'</th><th>'+obj.price+'</th><th>'+obj.remark+'</th></th>';
}else{
table = table + '<tr><td>'+obj.name+'</td><td>'+obj.type+'</td><td>'+obj.price+'</td><td>'+obj.remark+'</td></tr>';
}
}
$("#title").text(title);
$(".datalist").append(table);
$(".datalist > tbody > tr:odd").addClass('yellow');
});
</script>
<style type="text/css">
*{ margin:0px; padding:0px;}
img{ border:none; max-width:100%;}
.top{ width:100%; height:93px; background:#d91417; text-align:center; vertical-align:middle;}
.maincontent{ font-size:12px; width:100%;}
.title{ margin-top:15px; margin-bottom:15px; font-weight:bold;}
.datalist{ border:1px solid #999999; border-collapse:collapse;}
.datalist th, .datalist td{ border:1px solid #999999; text-align:center; height:30px; line-height:30px; }
.yellow{ background:#fff798;}
</style>
</head>
<body>
<div class="content">
<div class="top"> <img src="images/logo01.png" /> </div>
<div class="maincontent" align="center">
<table width=100% border="0" style=" font-weight:bold; height:50px; line-height:50px;">
<tr>
<td id="title" align="center"></td>
</tr>
</table>
<div >
<table class="datalist" width="100%">
</table>
<table width=100% border="0">
<tr>
<td style="height:30px; line-height:30px; font-weight:bold;" align="center">咨询电话:400118</td>
</tr>
<tr>
<td style="height:30px; line-height:30px; font-weight:bold;" align="center">本报价单仅供参考,实际价格以下单实时价格为准。</td>
</tr>
</table>
</div>
</div>
</div>
</body>
</html>