先准备一个Excel表格
前端页面,上传按钮
<style>
table tr td{
border:1px solid ;
}
</style>
<button id="chuan">点击上传1</button>
<input type="file" hidden id="moban1" value="上传模板" onchange="File1()"><br><table>
<thead>
<tr>
<td>系统编号</td>
<td>发起人</td>
<td>类型</td>
<td>类型编号</td>
<td>主题</td>
<td>发起日期</td>
<td>反馈截止日期</td>
<td>关闭日期</td>
<td>当前状态</td>
</tr>
</thead>
<tbody id="daoru">
</tbody>
</table>
<div id="QWER"></div>
js部分
//点击上传
$("#chuan").click(function () {
$("#moban1").click()
})
//文件上传
function File1(type) {
var formData = new FormData();
var aaa = $("#moban1")[0];
var filePath = URL.createObjectURL(aaa.files[0])
//上传之后会有一个预览效果
$("#QWER").append("<a href=\"" + filePath + "\">" + aaa.files[0].name+"</a><br/>")
formData.append("file", aaa.files[0]);
$.ajax({
url: '@Url.Action("Moban2", "Home")',
type: "POST",
cache: false,
processData: false,
contentType: false,
data: formData,
success: function (datee) {
for (var i in datee.biao) {
$("#daoru").append(
" <tr>"
+ "<td>" + datee.biao[i].id + "</td>"
+ "<td>" + datee.biao[i].name + "</td>"
+ "<td>" + datee.biao[i].lei + "</td>"
+ "<td>" + datee.biao[i].leiid + "</td>"
+ "<td>" + datee.biao[i].theme + "</td>"
+ "<td>" + datee.biao[i].playdata + "</td>"
+ "<td>" + datee.biao[i].lowdata + "</td>"
+ "<td>" + datee.biao[i].closedata + "</td>"
+ "<td>" + datee.biao[i].state + "</td>"
+ "</tr >"
)
}
})
}
上传预览
后端代码
引用NPOI
using System.IO;
using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;
定义的实体
public class ManagerInfo
{
public string id { get; set; }
public string name { get; set; }
public string lei { get; set; }
public string leiid { get; set; }
public string theme { get; set; }
public string playdata { get; set; }
public string lowdata { get; set; }
public string closedata { get; set; }
public string state { get; set; }
}
//导入Excel表格
public ActionResult Moban2()
{
//创建一个实体接受表字段
List<ManagerInfo> biao = new List<ManagerInfo>();
var biao1 = new ManagerInfo();
HttpPostedFileBase file = Request.Files[0];
string extension = Path.GetExtension(file.FileName);
string fileName = Path.GetFileName(file.FileName);
var excelUploadPath = "~/Files/Upload/file/" + fileName;
//没有路径的话创建一个文件夹
if (!Directory.Exists(Path.GetDirectoryName(Server.MapPath(excelUploadPath))))
{
Directory.CreateDirectory(Path.GetDirectoryName(Server.MapPath(excelUploadPath)));
}
//1.1读取文件 路径
FileStream fs = new FileStream(Server.MapPath(excelUploadPath), FileMode.Create, FileAccess.Write);
file.InputStream.CopyTo(fs);
fs.Close();
//读取文件
FileStream file1 = new FileStream(Server.MapPath(excelUploadPath), FileMode.Open, FileAccess.Read);
HSSFWorkbook wb = new HSSFWorkbook(file1);
HSSFSheet sheet = (HSSFSheet)wb.GetSheet("sheet");
//判断数据多少行
var rownum = sheet.LastRowNum;
//循环行
for (int j = 2; j <= rownum; j++)
{
IRow row = sheet.GetRow(j);
biao1.id = sheet.GetRow(j).GetCell(0).ToString();
biao1.name = sheet.GetRow(j).GetCell(1).ToString();
biao1.lei = sheet.GetRow(j).GetCell(2).ToString();
biao1.leiid = sheet.GetRow(j).GetCell(3).ToString();
biao1.theme = sheet.GetRow(j).GetCell(4).ToString();
biao1.playdata = sheet.GetRow(j).GetCell(5).ToString();
biao1.lowdata = sheet.GetRow(j).GetCell(6).ToString();
biao1.closedata = sheet.GetRow(j).GetCell(7).ToString();
biao1.state = sheet.GetRow(j).GetCell(8).ToString();
biao.Add(biao1);
}
file1.Close();
return Json(new { code = 200, biao });
}
最终效果
代码命名不规范,后续会改进