How to insert a new column and data in newly created column in excel in .Net? RRS feed

  • Question

  • User-184135847 posted

    I have following scenario,


    test.xls have following columns,

      column1      column2         column3

    1   a                 a                     a

    2    b                b                     b

    3    c                 c                     c


    Now, I need to insert a new column in test.xls programatically as follow,

        column0      column1         column2               column3

    1   test                a                     a                            a

    2    test               b                     b                             b

    3    test               c                     c                              c


    Can anyone please help me in .Net using C#.

    I appreciate help in this.



    Monday, January 25, 2010 8:05 PM

All replies

  • User1611391320 posted

    string str = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath("Xls/" + System.IO.Path.GetFileName(FileUploadExcel.PostedFile.FileName)) + ";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=1\";";
            OleDbConnection conn = new OleDbConnection();

                #region Get Excel Sheet

                conn.ConnectionString = str;

    you have opened the excel connection now use  oled command to perform the action  like for select we do

    OleDbCommand cmd = new OleDbCommand("select * from " + GetExcelWorkSheet(Server.MapPath("Xls/" + System.IO.Path.GetFileName(FileUploadExcel.PostedFile.FileName)), System.IO.Path.GetFileName(FileUploadExcel.PostedFile.FileName), 0) + "");
                cmd.Connection = conn;
                DataSet ds = new DataSet(); ;
                OleDbDataAdapter adp = new OleDbDataAdapter();
                adp.SelectCommand = cmd;
                adp.Fill(ds, "Product");

    Wednesday, February 3, 2010 5:31 AM