【问题标题】:httpWebRequest Download and parse excel file?httpWebRequest 下载并解析excel文件?
【发布时间】:2014-07-15 21:49:38
【问题描述】:

我正在寻找一种使用 httpWebRequest 从 url 下载 excel 文件并以某种方式解析它的方法 - 这是否意味着将其转换为 .csv 文件,以便我可以简单地使用 TextFieldParser 或将其保留为 excel 文件,我不知道。

private byte[] GetExcelFile()
        {
            var httpWebRequest = (HttpWebRequest)WebRequest.Create("url_To_Excel_File"); 
            httpWebRequest.ContentType = "application/vnd.ms-excel";
            httpWebRequest.Method = "GET";

            var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();

            try
            {

                using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
                {
                    var contents = streamReader.ReadToEnd();
                    return contents;
                }

            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                throw;
            }
         }

我的理解是contents 应该是一个字节数组?如何正确下载此 excel 文件并解析响应?

【问题讨论】:

  • 为什么不使用 WebClient 类呢?使用 DownloadData() 下载文件 - 它返回一个字节数组。如果您需要阅读该文件,我建议使用 OleDB 对其进行查询。见这里:stackoverflow.com/questions/18511576/…
  • 能否提供在 WebClient 类中使用 DownloadData() 的示例代码?
  • 您无法使用 oledb 从内存流中读取 excel,您必须事先将其保存到文件中。
  • Excel 生成的 xsl 文件中的 Excel 文件通常是二进制形式,没有 excel/oledb 将无法解析。但是,如果你得到一个 CSV 文件,你可以在内存中解析它。但是excel将其保存到磁盘并使用OleDB读取它。

标签: c# excel


【解决方案1】:

如何使用 WebClient 类下载 Excel 文件,使用 DownloadFile() 而不是 DownloadData()(更简单)。

string destFilename = HttpContext.Current.Server.MapPath("~/YourExcelFile.xlsx");
WebClient client = new WebClient();
client.DownloadFile("http://www.blabla.com/YourExcelFile.xlsx", destFilename);

这应该将文件下载到应用程序的根目录。

下一步是读取文件。根据我的经验,以编程方式读取 Excel 文件的最简单方法就是使用 SQL/OleDB 进行查询。

如何将文件的第一页读入数据表的示例:

string connectionString = GetExcelConnectionString(destFilename);

string sheetName = GetFirstSheet(filePath);

OleDbConnection excelCon = new OleDbConnection(connectionString);
OleDbDataAdapter adapter = new OleDbDataAdapter(String.Format("select * from [{0}]", sheetName), excelCon);

DataTable dataTable = new DataTable("ExcelDocument");
adapter.Fill(dataTable);

获取连接字符串的辅助函数:

// Currently only supports Excel 12.0 (.xlsx files), the connection string for .xls is a little different.
public string GetExcelConnectionString(string filePath)
{
    return "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + filePath + ";Extended Properties=Excel 12.0;";
}

帮助读取文件中第一张工作表的名称:

public string GetFirstSheet(string filePath)
{
    string connectionString = GetExcelConnectionString(filePath);

    using (OleDbConnection connection = new OleDbConnection(connectionString))
    {
        connection.Open();
        DataTable dtSheet = connection.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);

        return dtSheet.Rows[0]["TABLE_NAME"].ToString();
    }

    return String.Empty;
}

现在您应该将文件的内容保存在 DataTable 中,这样就可以轻松地对数据进行任何操作。

请注意,这只是给您一个想法,可能并不完美。您可能希望在处理后进行清理 - 例如,从应用程序的根目录中删除 excel 文件。

【讨论】:

    猜你喜欢
    • 2018-03-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-01
    • 1970-01-01
    • 2011-10-10
    • 1970-01-01
    相关资源
    最近更新 更多