【问题标题】:Inserting value into excel via asp.net app通过 asp.net 应用程序将值插入 excel
【发布时间】:2018-12-07 15:52:43
【问题描述】:

我有一个电子表格,需要接受来自 asp.net 应用程序的数据。

我已经编写了一些 c#,允许我从另一个电子表格中读取数据,但现在我想从文本框中输入一些数据。

        protected void Button2_Click(object sender, EventArgs e)
    {
        OleDbConnection conn = new OleDbConnection();
        string path = Server.MapPath("LOG_TEST.xlsx");
        String connString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties='Excel 12.0;IMEX=1;'";
        conn.ConnectionString = connString;
        conn.Open();
        OleDbCommand cmd = new OleDbCommand("INSERT INTO [Sheet1$] ([NAME], [MOBILE], [EMAIL]) VALUES('" + TextBox2.Text + "','" + TextBox3.Text + "','" + TextBox4.Text + "')",conn);
        OleDbDataReader rd = cmd.ExecuteReader();
        conn.Close();
    }

只需在电子表格中插入 3 个值。

我得到的错误如下。

我相信问题出在

OleDbDataReader rd = cmd.ExecuteReader();

但不确定用什么替换它。

【问题讨论】:

  • 不能解决我的问题。欢呼吧。
  • 使用 ExecudeNonQuery 而不是 ExecuteReader
  • 还查看了电子表格中的权限,一切正常。
  • 使用 ExecuteNonQuery 时仍然收到相同的错误消息

标签: c# asp.net excel insert


【解决方案1】:

我建议像下面那样简化它: 使用本地驱动器 C: 中的电子表格,更新 connString 中目录的其余部分。在您的 INSERT QUERY 中使用字符串值,您可以稍后对其进行更新。我们只是想确保它首先起作用!顺便说一句,我没有测试这段代码,但我认为它应该可以工作。

protected void Button2_Click(object sender, EventArgs e)
{
    string connString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + "C:\\SPECIFY HERE\\LOG_TEST.xlsx" + ";Extended Properties=\"Excel 12.0;ReadOnly=False;HDR=Yes;\"";

 string query ="INSERT INTO [Sheet1$] (NAME, Mobile, Emails) VALUES ('Bob', '1', 'Whatever@mail')";

 OleDbConnection con = new OleDbConnection(connString);
 OleDbCommand cmd = new OleDbCommand(query, con);

 con.Open();
 cmd.ExecuteNonQuery();


}

我假设您可以编写一个函数来打开您的 excel 文件,例如

//Make sure you add these two references. 
using Microsoft.Office.Interop
using Excel = Microsoft.Office.Interop.Excel

//Call this before your insert
static void FileOpen() {
    string path = "Path.xlsx"
    var excel = new Excel.Application
    excel.Visible=True

    Excel.Workbooks books = excel.Workbooks;
    Excel.Workbook sheet = books.Open(path);
}

【讨论】:

  • 我能看到的唯一问题是电子表格必须打开才能插入行。
【解决方案2】:

我已经使用 Excel 文件好几年了,对我来说最简单和最可靠的方法是使用 NPOI for .NET。它以免费 NuGet 包的形式提供https://www.nuget.org/packages/NPOI/

示例:

XSSFWorkbook wb1 = null;
using (var file = new FileStream("D:\\banding.xlsx", FileMode.Open, 
FileAccess.ReadWrite)) 
{
  wb1 = new XSSFWorkbook(file);
}

wb1.GetSheetAt(0).GetRow(0).GetCell(0).SetCellValue("Sample");

using (var file2 = new FileStream("D:\\banding2.xlsx", FileMode.Create, 
       FileAccess.ReadWrite)) {
         wb1.Write(file2);
         file2.Close();
      }

取自 Muthukrishnan Ramasamy 在https://social.msdn.microsoft.com/Forums/vstudio/en-US/d1c5e191-135b-45c0-9f88-cc3e02849257/npoi-how-to-write-to-an-xlsx-excel-file?forum=csharpgeneral 上的回答

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-03
    • 1970-01-01
    • 2019-03-22
    • 1970-01-01
    相关资源
    最近更新 更多