Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. how to write to a specific range in excel ? [modified]

how to write to a specific range in excel ? [modified]

Scheduled Pinned Locked Moved C#
tutorialquestion
2 Posts 1 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    tonyonlinux
    wrote on last edited by
    #1

    For some reason this code is not writing to my excel file and I can't figure out why. I have a premade template and it has a worksheet in it called bsheet and I also took and selected my range and hit insert/name and gave it the name btable I then put this in a button click event to test but the xls is not effected what might i be doing wrong?

    private void button1_Click(object sender, EventArgs e)
    {
    string connectionString = "Provider=Microsoft.Jet.OleDb.4.0; data source=c:\\BookList.xls; Extended Properties=Excel 8.0;";
    string selectString = "INSERT INTO btable VALUES('12345′, 'Acme Inc', 'Acme Way', 'CA')";

            OleDbConnection con = new OleDbConnection(connectionString);
            OleDbCommand cmd = new OleDbCommand(selectString, con);
    
            try
            {
                con.Open();
                cmd.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            finally
            {
                con.Dispose();
            }
        }
    

    modified on Wednesday, March 3, 2010 1:52 PM

    T 1 Reply Last reply
    0
    • T tonyonlinux

      For some reason this code is not writing to my excel file and I can't figure out why. I have a premade template and it has a worksheet in it called bsheet and I also took and selected my range and hit insert/name and gave it the name btable I then put this in a button click event to test but the xls is not effected what might i be doing wrong?

      private void button1_Click(object sender, EventArgs e)
      {
      string connectionString = "Provider=Microsoft.Jet.OleDb.4.0; data source=c:\\BookList.xls; Extended Properties=Excel 8.0;";
      string selectString = "INSERT INTO btable VALUES('12345′, 'Acme Inc', 'Acme Way', 'CA')";

              OleDbConnection con = new OleDbConnection(connectionString);
              OleDbCommand cmd = new OleDbCommand(selectString, con);
      
              try
              {
                  con.Open();
                  cmd.ExecuteNonQuery();
              }
              catch (Exception ex)
              {
                  Console.WriteLine(ex.Message);
              }
              finally
              {
                  con.Dispose();
              }
          }
      

      modified on Wednesday, March 3, 2010 1:52 PM

      T Offline
      T Offline
      tonyonlinux
      wrote on last edited by
      #2

      well i got it sorta working. The thing I'm having trouble with is one of the cells is merged D to G if i insert values "('A,'B','C','D')"; i get an error saying it can't expand. I also tried putting '' for column e,f,g and that didn't work either. Any idea how to insert a value into the merge cell? i tried to find some info online about this but came up try.

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups