怎么使用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

转载于:https://www.cnblogs.com/net064/p/10277584.html

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值