怎么使用Aspose.Cells讀取excel 轉化為Datatable


說明:vs2012 asp.net mvc4 c#

        使用Aspose.Cells 讀取Excel 轉化為Datatable

1、HTML前端代碼

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm4.aspx.cs" Inherits="MvcAppTest.WebForm4" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title>獲取文件本地路徑</title> 
</head>
<body>
    <form action="Home/GetexcelNew" method="post" enctype="multipart/form-data">
        <input type="file" value="" name="files" />
        <input type="submit" value="提交" />
    </form>
</body>
</html>
   

2、Home控制器后台代碼

 [HttpPost] public void GetexcelNew(IEnumerable<HttpPostedFileBase> files) { Int32 count = files.Count(); foreach (var file in files) { if (file != null && file.ContentLength > 0) { string[] picName = file.FileName.Split('.'); if (picName[1] == "xls" || picName[1] == "xlsx") { DataTable dt = ReadExcel(file); } } } }
    public static System.Data.DataTable ReadExcel(HttpPostedFileBase file) { Workbook book = new Workbook(file.InputStream); Worksheet sheet = book.Worksheets[0]; Cells cells = sheet.Cells; return cells.ExportDataTableAsString(0, 0, cells.MaxDataRow + 1, cells.MaxDataColumn + 1, true); }

注意:后台代碼 要引用:using Aspose.Cells;

3、引用文件下載地址

鏈接:https://pan.baidu.com/s/1TTcrTIVO4nkC2TH27whFwQ
提取碼:2w1j


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM