vb.net datagrid to xls

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Garima12
    New Member
    • Mar 2007
    • 58

    vb.net datagrid to xls

    I want to fill the data from datagrid control of vb.net in xls file. Please suggest some code to fill xls from datagrid, not with dataset as I can't because I am filling gridview using some other tool.
  • kenobewan
    Recognized Expert Specialist
    • Dec 2006
    • 4871

    #2
    Originally posted by Garima12
    I want to fill the data from datagrid control of vb.net in xls file. Please suggest some code to fill xls from datagrid, not with dataset as I can't because I am filling gridview using some other tool.
    What other tool are you using?

    Comment

    • Plater
      Recognized Expert Expert
      • Apr 2007
      • 7872

      #3
      Don't DataTable and DataSets have a WriteXML() and WriteXmlSchema( ), could those be used to help?
      I don't think there is a native way in .NET to create an excel file.
      You could make a CSV pretty easy from it though.

      Comment

      • Garima12
        New Member
        • Mar 2007
        • 58

        #4
        can you post help regarding creating csv file?

        Originally posted by Plater
        Don't DataTable and DataSets have a WriteXML() and WriteXmlSchema( ), could those be used to help?
        I don't think there is a native way in .NET to create an excel file.
        You could make a CSV pretty easy from it though.

        Comment

        • Plater
          Recognized Expert Expert
          • Apr 2007
          • 7872

          #5
          Loop through your data, each row is a line, each column gets a comma between it.
          If there are commas in your data, wrap the whole thing in quotes.
          If there are quotes in you data, wrap it in double quotes.

          Check the internet for more CSV rules.

          Comment

          • Curtis Rutland
            Recognized Expert Specialist
            • Apr 2008
            • 3264

            #6
            Originally posted by Plater
            Don't DataTable and DataSets have a WriteXML() and WriteXmlSchema( ), could those be used to help?
            I don't think there is a native way in .NET to create an excel file.
            You could make a CSV pretty easy from it though.
            There may be no native way, but there is a way using Interop.

            Originally posted by Garima12
            can you post help regarding creating csv file?
            It's fairly simple. You need to include System.IO, and then just create the file:
            Code:
            FileStream fs = new FileStream(@"c:\dev\test.csv", FileMode.Create);
            StreamWriter sw = new StreamWriter(fs);
            /////////
            //This section: replace.  Loop through your data, inserting commas between columns and newlines between rows.
            sw.WriteLine("name,number");
            sw.WriteLine("Curtis,10");
            /////////
            sw.Close();
            fs.Close();
            //always close your streams
            And follow Plater's advice, find out more on rules of CSVs.

            Comment

            • Plater
              Recognized Expert Expert
              • Apr 2007
              • 7872

              #7
              Originally posted by insertAlias
              There may be no native way, but there is a way using Interop.
              I guess I figured that wasn't an option. I assume too much sometimes.

              Comment

              Working...