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. Create a excelfile in C# - save with version

Create a excelfile in C# - save with version

Scheduled Pinned Locked Moved C#
questionhelpcsharpcomtutorial
4 Posts 2 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.
  • M Offline
    M Offline
    Mschauder
    wrote on last edited by
    #1

    Hey guys =), I want to create a ".xls" and ".xlsx" file in C#2008. Creating && saving is no problem, but how can I close it? I think I've a bug in my code...   :doh:

    //this is how to create and save
    Microsoft.Office.Interop.Excel.Application excelApp = new ApplicationClass();
    Microsoft.Office.Interop.Excel.Workbook newWorkbook = excelApp.Workbooks.Add(Microsoft.Office.Interop.Excel.XlWBATemplate.xlWBATWorksheet);
    Microsoft.Office.Interop.Excel.Sheets excelSheets = newWorkbook.Worksheets;
    string currentSheet = "Tabelle1"; //german name
    Microsoft.Office.Interop.Excel.Worksheet worksheet = (Microsoft.Office.Interop.Excel.Worksheet)excelSheets.get_Item(currentSheet);

    worksheet.SaveAs(Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "\\SaveAsTest.xlsx", t, t, t, t, t, t, t, t, t);

    but now the question is... "how can I close this worksheet?" :confused: a ".close" property doesn't exist... I read, that I can close the workbook but the workbook uses a already existing file. Is it possible to assign the new created to a workbook? greets Mschauder   :-D

    L 1 Reply Last reply
    0
    • M Mschauder

      Hey guys =), I want to create a ".xls" and ".xlsx" file in C#2008. Creating && saving is no problem, but how can I close it? I think I've a bug in my code...   :doh:

      //this is how to create and save
      Microsoft.Office.Interop.Excel.Application excelApp = new ApplicationClass();
      Microsoft.Office.Interop.Excel.Workbook newWorkbook = excelApp.Workbooks.Add(Microsoft.Office.Interop.Excel.XlWBATemplate.xlWBATWorksheet);
      Microsoft.Office.Interop.Excel.Sheets excelSheets = newWorkbook.Worksheets;
      string currentSheet = "Tabelle1"; //german name
      Microsoft.Office.Interop.Excel.Worksheet worksheet = (Microsoft.Office.Interop.Excel.Worksheet)excelSheets.get_Item(currentSheet);

      worksheet.SaveAs(Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "\\SaveAsTest.xlsx", t, t, t, t, t, t, t, t, t);

      but now the question is... "how can I close this worksheet?" :confused: a ".close" property doesn't exist... I read, that I can close the workbook but the workbook uses a already existing file. Is it possible to assign the new created to a workbook? greets Mschauder   :-D

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      It looks like you need the Application.Quit()[^] method; don't you just love Microsoft?

      MVP 2010 - are they mad?

      M 1 Reply Last reply
      0
      • L Lost User

        It looks like you need the Application.Quit()[^] method; don't you just love Microsoft?

        MVP 2010 - are they mad?

        M Offline
        M Offline
        Mschauder
        wrote on last edited by
        #3

        Hey, worked fine! Now the next problem... my computer uses office 2007, I can open the ".xlsx" && ".xls" file smoothly... ;P But if I use the laptop with office 2003... sure I can't open the ".xlsx", but why can't I open the ".xls"???:confused: Is there a possibility to save the file, with the natural format for office 2003 (.xls) :confused: greets Mschauder :-O

        M 1 Reply Last reply
        0
        • M Mschauder

          Hey, worked fine! Now the next problem... my computer uses office 2007, I can open the ".xlsx" && ".xls" file smoothly... ;P But if I use the laptop with office 2003... sure I can't open the ".xlsx", but why can't I open the ".xls"???:confused: Is there a possibility to save the file, with the natural format for office 2003 (.xls) :confused: greets Mschauder :-O

          M Offline
          M Offline
          Mschauder
          wrote on last edited by
          #4

          I answer now my own question (sorry!): just use: t means

          object t = Type.Missing;

          //XlFileFormat.xlExcel8 suits to .xls format
          worksheet.SaveAs("Test.xls", XlFileFormat.xlExcel8, t, t, t, t, t, t, t, t);

          and saving as office2007:

          worksheet.SaveAs("Test.xls", t, t, t, t, t, t, t, t, t);

          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