10

我需要允许此 ASP.NET Web 应用程序的用户上传特定格式的 Excel 电子表格,用电子表格中的数据填充数组,并将数组绑定到 Oracle 存储过程以进行验证并插入数据库。我必须能够从 Excel 电子表格中读取数据,但不能将其保存到 Web 服务器的硬盘上。这是我不知道该怎么做的部分。这是一个简单的代码示例。

<%--ASP.NET Declarative--%>
<asp:FileUpload ID="FileUpload1" runat="server" />
<asp:Button ID="Button1" runat="server" Text="Send File" OnClick="Button1_Click" />

// C# Code-Behind
protected void Button1_Click(object sender, EventArgs e) {
    var postedFile = FileUpload1.PostedFile;

    // ... Read file in memory and put in format to send to stored procedure ...

}

谁能帮我这个?我感谢任何人的考虑。

谢谢,
加布

4

6 回答 6

8

我在 Codeplex 上找到了一个很棒的轻量级开源 API,用于执行此操作,称为 ExcelDataReader。

它可以将 excel 文件的输入流转换为System.Data.DataSet对象(可能使用 BIFF 规范进行解析)。

这是链接:

http://www.codeplex.com/ExcelDataReader

这是一个代码示例:

<%--ASP.NET Declarative--%>
<asp:FileUpload ID="FileUpload1" runat="server" />
<asp:Button ID="Button1" runat="server" Text="Send File" OnClick="Button1_Click" />
<asp:GridView ID="GridView1" runat="server" />

// C# Code-Behind
protected void Button1_Click(object sender, EventArgs e) {
    // the ExcelDataReader takes a System.IO.Stream object
    var excelReader = new ExcelDataReader(FileUpload1.FileContent);
    FileUpload1.FileContent.Close();

    DataSet wb = excelReader.WorkbookData;
    // get the first worksheet of the workbook
    DataTable dt = excelReader.WorkbookData.Tables[0];

    GridView1.DataSource = dt.AsDataView();
    GridView1.DataBind();
}
于 2008-11-06T17:23:56.543 回答
4

使用 FileUpload1。文件内容流。我猜你的 Excel 库可以直接处理流。

于 2008-11-04T16:25:30.263 回答
1

也许看看 csvreader,它读取 csv、xls 和 xlsx:

http://www.csvreader.com

于 2008-11-04T23:38:43.743 回答
1

Excel 的 COM 库不支持从文件以外的其他源加载文件。但是存在很多第三方组件,可以让您读取/写入excel文件。

否则,您可以在[MS-XLS]:Excel Binary File Format (.xls) Structure Specification中查看有关 th XLS 文件格式的文档。

或者,您可以使用与 Sharepoint Server 中相同的办公文件处理方式。请参阅Microsoft.Office.Excel.Server.WebServices 命名空间

于 2008-11-04T16:58:55.237 回答
0

这是我最近一直在玩的东西。

检查这篇文章:Write an excel workbook to a memory stream .NET

它指向 Carlos Aguilar Mares 的一个很棒的库,它允许您将 Excel 工作簿作为 XML 使用。

ExcelXMLWriter

您不需要在服务器上安装 Excel(当您通过 Web 访问它时,这有点破坏 MS 许可)。

您可以使用将 Excel 工作簿作为流加载Workbook.Load(stream)

于 2008-11-04T17:39:54.140 回答
-1

您可以让您的用户上传 CSV 文件吗?处理纯文本文件会容易得多。我之前遇到过类似的问题,我问过用户,他们都很好,为我节省了大量的工作。

祝你好运。

于 2008-11-04T18:27:58.193 回答