1、使用NPOI,可以在没有安装office的设备上读wiod、office。
2、本文只能读取.docx后缀的文档。
3、MVC架构中,上传文件只能使用form表单提交,转到控制器后要依次实现文件上传、打开文件、读取文件内容。
4、当读取文档中的表格时,逐行、逐单元格读取。
XCHTML:
<form id="form1" method="post" action="@Url.Action("Add","MeetRecord")" enctype="multipart/form-data">
<input type="file" name="files" id="files" οnchange="ReadeWordTable(this)" />
<input type="submit" value="提交" />
</form>
● Url.Action("Add","MeetRecord")表单提交到MeetRecordControl控制器里面的Add方法。
● type为file的input框里面一定要有name属性,后台在接受文件时,是通过这个name字段接收的,如果不定义name属性,或者name的值与控制器中接收的参数名不一致,会出现问题。
● 因为使用form表单提交,后台只能返回一个页面,因此我在type为file的input框里添加了一个onchange事件,用来验证所选择的文件,是否符合要求。
● 下面的方法就是用来验证文件是否符合要求。
<script>
function ReadeWordTable(target) {
var isIE = /msie/i.test(navigator.userAgent) && !window.opera;
var fileSize = 0;
var filetypes = [".docx"];
var filepath = target.value;
var filemaxsize = 1024 * 2;//2M
if (filepath) {
var isnext = false;
var fileend = filepath.substring(filepath.lastIndexOf("."));
if (filetypes[0] == fileend) {
isnext = true;
}
if (!isnext) {
alert("只能上传.docx类型文件!");
target.value = "";
return false;
}
} else {
return false;
}
if (isIE && !target.files) {
var filePath = target.value;
var fileSystem = new ActiveXObject("Scripting.FileSystemObject");
if (!fileSystem.FileExists(filePath)) {
alert("附件不存在,请重新输入!");
return false;
}
var file = fileSystem.GetFile(filePath);
fileSize = file.Size;
} else {
fileSize = target.files[0].size;
}
var size = fileSize / 1024;
if (size > filemaxsize) {
alert("附件大小不能大于" + filemaxsize / 1024 + "M!");
target.value = "";
return false;
}
if (size <= 0) {
alert("附件大小不能为0M!");
target.value = "";
return false;
}
}
</script>
c#后台实现:
using System;
using System.Data;
using System.IO;
using System.Text;
using System.Web;
using System.Web.Mvc;
using NPOI.XWPF.UserModel;
[HttpPost]
public ActionResult Add(HttpPostedFileWrapper files)
{
HttpServerUtility server = System.Web.HttpContext.Current.Server;
string save_Path = server.MapPath("..\\Word\\");
string fileName = files.FileName;
files.SaveAs(save_Path + fileName);
ViewBag.fileName = ReadWordText(fileName);
return View();
}
读取文档里面的表格
public string ReadWordText(string fileName)
{
string filePath = "..\\Word\\" + fileName;
HttpServerUtility server = System.Web.HttpContext.Current.Server;
string endPath = server.MapPath(filePath);
string fileText = string.Empty;
StringBuilder sbFileText = new StringBuilder();
#region 打开文档
XWPFDocument document = null;
using (FileStream file = new FileStream(endPath, FileMode.Open))
{
document = new XWPFDocument(file);
}
#endregion
#region 表格
foreach (XWPFTable table in document.Tables)
{
foreach(XWPFTableRow row in table.Rows)
{
foreach (XWPFTableCell cell in row.GetTableCells())
{
sbFileText.Append(cell.GetText()+"|");
}
}
}
#endregion
return sbFileText.ToString();
}
- 在后台接收文件时,一定要添加[HttpPost],否则会报错。
- 在Add方法中类型要ActionResult ,传的参数类型应该为HttpPostedFileWrapper 文件名应该与type为file的input的name属性值相同。
- 因为我需要获得文档表格里面的内容,在页面上显示出来,为了方便在前台读取每个表格里面的内容,用‘|’字符将表格里面的内容相互分割开。
- 在页面上使用Razor,进行接受,在对其进行处理,代码如下所示:
string fileName = ViewBag.fileName; string[] FileNameArr = null; if (fileName != "" && fileName != null) { FileNameArr = fileName.Split('|'); }