zoukankan      html  css  js  c++  java
  • asp.net mvc或者其他程序无法打开excel——解决方案,C#处理Excel文件

    问题描述:今天处理Excel时遇到一个问题,本地使用Microsoft.Jet.OLEDB.4.0处理,正常完成了需求,

    上传到服务器后发生了异常,通过排查发现问题出现在对Excel文件的读取上,然后推测是因为

    没有安装对应的驱动,然后在微软官网查到了Microsoft.ACE.OLEDB.12.0;这个驱动,

    服务器上安装后变成功解决了此问题,同时需要注意,连接字符串需要改成Microsoft.ACE.OLEDB.12.0;

    示例

    public static DataSet ExcelToDS(string Path)
            {
                //string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + Path + ";" + "Extended Properties=Excel 8.0;";
                //发布后开启此注释
                string strConn = "Provider=Microsoft.ACE.OLEDB.12.0;" + "Data Source=" + Path + ";" + "Extended Properties=Excel 8.0;";
                OleDbConnection conn = new OleDbConnection(strConn);
                conn.Open();
                string strExcel = "";
                OleDbDataAdapter myCommand = null;
                DataSet ds = null;
                strExcel = "select * from [sheet1$]";
                myCommand = new OleDbDataAdapter(strExcel, strConn);
                ds = new DataSet();
                myCommand.Fill(ds, "table1");
                return ds;
            }
  • 相关阅读:
    Best Time to Buy and Sell Stock
    Permutations II
    数组中最大和的子数组
    基于Socket.IO的Client封装
    Java中的ThreadLocal功能演示
    基于WebSocket的client封装
    Socket接口开发和测试实践
    自动化测试用例的原子性
    [CF1477C] Nezzar and Nice Beatmap
    [CF1477B] Nezzar and Binary String
  • 原文地址:https://www.cnblogs.com/ives/p/6854571.html
Copyright © 2011-2022 走看看