0

I have a spreadsheet that need to accept data from a asp.net app.

I have wrote some c# that allows me to read data from another spread sheet but now i want to input some data from text boxes.

        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();
    }

Just imple insert 3 values into the spread sheet.

error im getting is below.

enter image description here

I believe the issue is around

OleDbDataReader rd = cmd.ExecuteReader();

but not sure what to replace it with.

5
  • First search on google: stackoverflow.com/questions/34922325/… Commented Dec 7, 2018 at 15:56
  • doesn't solve my issue. cheers though. Commented Dec 7, 2018 at 16:07
  • use ExecudeNonQuery rather than ExecuteReader Commented Dec 7, 2018 at 16:13
  • also looked at the permissions in the spreadsheet and all looks fine. Commented Dec 7, 2018 at 16:13
  • Still getting the same error message when using ExecuteNonQuery Commented Dec 7, 2018 at 16:19

2 Answers 2

1

I recommend simplifying it just as I did it below: Use a spreadsheet from your Local Drive, C:, update the rest of the directory in the connString. Use string values in your INSERT QUERY, you can update it later. We just want to be sure that it works first! I didn't test this code btw, but I think it should work.

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();


}

I assume you can write a function to open your excel file something like

//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);
}
Sign up to request clarification or add additional context in comments.

1 Comment

Only issue i can see is that the spreadsheet has to be open in order for it to insert the row.
0

I have been working with Excel files for several years now, and the easiest and most reliable way for me has been using NPOI for .NET. It's available as a free NuGet package https://www.nuget.org/packages/NPOI/

Sample:

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();
      }

Taken from Muthukrishnan Ramasamy's answer on 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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.