c# ASP.NET MVC模式 WPS的匯入與匯出的實現
阿新 • • 發佈:2018-11-03
前提準備:
1.想要成功進行WPS的匯入與匯出,你得先下載WPS,然後找到etapi.dll檔案(路徑:\Kingsoft\WPS Office\10.1.0.7520\office6),WPS安裝路徑看你們具體的安裝路徑。
2.然後在自己的ASP.NET MVC專案下的引用項右鍵,新增引用,把etapi.dll新增進去。
、
點選瀏覽進行新增
、
新增完就可以開始寫程式碼了。
前臺程式碼:
<form action="/Home/ImportWPS" method="post" enctype="multipart/form-data"> <input type="file" name="file" /> <input type="submit" value="匯入" /> </form> <input type="button" onclick="ExportWPS()" value="匯出"> //需要引用JQuery @Scripts.Render("~/bundles/jquery") @Scripts.Render("~/bundles/bootstrap") <script> //匯出的JQ程式碼 function ExportWPS() { $.ajax({ url: "/Home/ExportWPS", data: {}, datatype: "json", type: "post", async: false, success: function (data) { alert("匯出成功!"); } }); } </script>
後臺程式碼:
WPS的索引從1開始,例如(1,1)。
需要引用Excel:Using Excel;
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using Excel; using System.IO; using System.Threading; using System.Diagnostics; using WPS檔案的讀寫.Models; namespace WPS檔案的讀寫.Controllers { public class HomeController : Controller { public ActionResult Index() { return View(); } //匯入 [HttpPost] public ContentResult ImportWPS(HttpPostedFileBase file) { List<List<string>> DataList = new List<List<string>>(); //先將選擇的檔案儲存到本地,然後從該檔案中獲取資料 System.IO.Stream stream = file.InputStream; string fileName = file.FileName; Excel.Application appli = new Application();//該物件代表 ET 應用程式。包含可返回最高階物件的屬性和方法。 try { if (fileName.LastIndexOf("\\") > -1) { fileName = fileName.Substring(fileName.LastIndexOf("\\") + 1); } var filePath = Server.MapPath(string.Format("~/UpFile", "File")); file.SaveAs(Path.Combine(filePath, fileName)); //WPS讀取檔案 Excel._Workbook wk = appli.Workbooks.Open(Path.Combine(filePath, fileName));//開啟檔案 Excel.Worksheet sheet = wk.Worksheets.get_Item(1); Excel.Range range = sheet.UsedRange;//獲取表格 //string data = ((Excel.Range)range.get_Item(1, 1)).Text; //獲取表格資料的方法 int rowCount = range.Rows.Count; //行數 int columCount = range.Columns.Count; //列數 string info = ""; //從第二行開始,表頭不匯入 for (int j = 2; j <= rowCount; j++) { info = ""; List<string> Temp = new List<string>(); for (int i = 1; i <= columCount; i++) { info += " " + ((Excel.Range)range.get_Item(j)).Cells[j, i].Text; Temp.Add(((Excel.Range)range.get_Item(j)).Cells[j, i].Text); } DataList.Add(Temp); System.Diagnostics.Debug.WriteLine(info);//輸出資訊到輸出界面 } appli.Quit();//退出ET應用程式 System.IO.File.Delete(Path.Combine(filePath, fileName)); return Content("<script>alert('匯入成功!');</script>"); } catch (Exception e) { System.Diagnostics.Debug.WriteLine("異常!"); appli.Quit();//退出ET應用程式 return Content("<script>alert('匯入失敗!');</script>"); } } //匯出 public void ExportWPS() { /*wsp excel匯出*/ Excel.Application appli = new Application(); Excel._Workbook wk = appli.Workbooks.Add(Type.Missing); Excel.Worksheet sheet = wk.ActiveSheet; List<List<string>> DataList = new List<List<string>>(); //表頭的設定 List<string> Header = new List<string>(); Header.Add("L1"); Header.Add("L2"); Header.Add("L3"); Header.Add("L4"); Header.Add("L5"); DataList.Add(Header); //資料的新增 for (var i = 0; i < 3; i++) { List<string> Temp = new List<string>(); Temp.Add("1"); Temp.Add("2"); Temp.Add("3"); Temp.Add("4"); Temp.Add("5"); DataList.Add(Temp); } //新增資料進檔案 //for迴圈的優化 int XLength = DataList.Count(); //行數 int YLength = DataList[0].Count(); //列數--已表頭為預設列數 for (var i = 0; i < XLength; i++) { for (var j = 0; j < YLength; j++) { sheet.Cells[i + 1, j + 1] = DataList[i][j]; } } //儲存 wk.SaveAs(Environment.GetFolderPath(Environment.SpecialFolder.Desktop)+"\\TempExportModel.xls", Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, XlSaveAsAccessMode.xlExclusive, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing); wk.Close(Type.Missing, Type.Missing, Type.Missing); System.Runtime.InteropServices.Marshal.ReleaseComObject(wk); System.Runtime.InteropServices.Marshal.ReleaseComObject(appli); } } }
專案下載路徑:https://download.csdn.net/download/laizhixue/10691046