none
Drucken über C# von definierten Excel Dateien bzw. Worksheets RRS feed

  • Frage

  • Ich bin gerade am verzweifeln, da ich es nicht schaffe den C# Code so zu schreiben, dass er eine Excel Datei (abgelegt unter D:\Test\Test1.xls) nicht öffnet und ausdruckt über PDF. Hoffe es kann mir einer von euch behilflich sein!! Vielen Dank schonmal im voraus.

    Mein Code sieht folgendermaßen aus:

    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Net;
    using System.Xml;
    
    using System.Globalization;
    
    namespace Microsoft.Office.Tools.Excel
    {
        class Program
        {
            public void Select(Object Replace);
            Globals.Sheet1.Select missing;
            //Auswahl eines Worksheets; in dem Fall Sheet1
    
            Excel.Worksheet
    
            this[Application.ActiveWorkbook.Sheets[] Select]
            {
                //Definition des zu druckenden Worksheets
                get
                {
                    Globals.Sheet1.PrintOut(1, 1, 2, true, missing, missing, missing, missing);
    
                    //Vorschau vor dem Drucken
    
                    Globals.Sheet1.PrintPreview(missing);
                    //Definition Range
                    object start = 0;
                    object end = 7;
                    Word.Range rng =this.Range(ref start, ref end);
                    rng.Select(Sheet1);
    
                    // Drucken des Worksheets
    
                    this.Range["A1", missing].Value2 = "123";
                    this.PrintOut(1, 2, 1, false, missing, true, false, missing);
    
                    // Druckername wird gesetzt
    
                    pd.PrinterSettings.PrinterName ="FreePDF";
    
                    // Ausdruck wird gestartet
    
                    pd.Print();
    
                    Console.ReadLine();
    
                }
    
            }
    
            private Range Range(ref object start, ref object end)
            {
                throw new NotImplementedException();
            }
    
        }
    }
    Samstag, 5. September 2009 10:24

Antworten

  • Hallo stumpfomat,

    Mit Hilfe von Workbook.ExportAsFixedFormat Methode kann man eine Excel Datei in das PDF- oder XPS-Format exportieren.

    Folgender CodeSchnipsel zeigt wie man eine Excel Datei als PDF exportiert.

     



     

    using System;
    using System.Windows.Forms;
    using System.Collections.Generic;
    using System.Text;
    using Microsoft.Office.Interop.Excel;
    
    namespace ConvertSheetCS
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                string paramSourceBookPath = @"C:\Robert\Test.xlsx";
                string paramExportFilePath = @"C:\Robert\Test.pdf";
                XlFixedFormatType paramExportFormat = XlFixedFormatType.xlTypePDF;
                //XlFixedFormatType paramExportFormat = XlFixedFormatType.xlTypeXPS;
                object paramMissing = Type.Missing;
    
                if (!System.IO.File.Exists(paramSourceBookPath))
                    throw new Exception("Die angegebene Excel Datei ist nicht vorhanden.");
    
                ApplicationClass excelApplication = new ApplicationClass();
    
                Workbook excelWorkBook = null;
    
                XlFixedFormatQuality paramExportQuality = XlFixedFormatQuality.xlQualityStandard;
                bool paramOpenAfterPublish = true;
                bool paramIncludeDocProps = true;
                bool paramIgnorePrintAreas = true;
                object paramFromPage = Type.Missing;
                object paramToPage = Type.Missing;
    
                try
                {
    
                    excelWorkBook = excelApplication.Workbooks.Open(paramSourceBookPath, paramMissing, paramMissing, paramMissing, paramMissing,
                        paramMissing, paramMissing, paramMissing, paramMissing, paramMissing, paramMissing, paramMissing, paramMissing, paramMissing, paramMissing);
    
    
                    if (excelWorkBook != null)
                        excelWorkBook.ExportAsFixedFormat(paramExportFormat, paramExportFilePath, paramExportQuality, paramIncludeDocProps,
                            paramIgnorePrintAreas, paramFromPage, paramToPage, paramOpenAfterPublish, paramMissing);
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
                finally
                {
                    if (excelWorkBook != null)
                    {
                        excelWorkBook.Close(false, paramMissing, paramMissing);
                        excelWorkBook = null;
                    }
    
                    if (excelApplication != null)
                    {
                        excelApplication.Quit();
                        excelApplication = null;
                    }
    
                    GC.Collect();
                    GC.WaitForPendingFinalizers();
                    GC.Collect();
                    GC.WaitForPendingFinalizers();
                }
            }
        }
    }


    Bitte nicht vergessen die Microsoft.Office.Interop.Excel Referenz einzufügen J

    Grüße,

    Robert

    Donnerstag, 17. September 2009 14:25
    Moderator
  • Hallo stumpfomat,

    damit das Codebeispiel oben funktioniert muss das Add-In zum Speichern als PDF / PDF oder XPS installiert sein. Dieses findest du unter http://www.microsoft.com/downloads/details.aspx?FamilyID=f1fc413c-6d89-4f15-991b-63b07ba5f2e5&DisplayLang=de bzw. http://www.microsoft.com/downloads/details.aspx?FamilyID=4d951911-3e7e-4ae6-b059-a2e79ed87041&DisplayLang=de

    Viele Grüße
    Jan
    Montag, 21. September 2009 11:11

Alle Antworten

  • Hallo,

    >>dass er eine Excel Datei (abgelegt unter D:\Test\Test1.xls) nicht öffnet und ausdruckt über PDF.

    kurze Nachfrage: Willst Du drucken (Excel in PDF) oder nicht ?

    Schöne Grüße
    Oliver

    P.S. Bitte formatiere Deine Code so das man ihn lesen kann
    Samstag, 5. September 2009 13:59
  • Hallo stumpfomat,

    Mit Hilfe von Workbook.ExportAsFixedFormat Methode kann man eine Excel Datei in das PDF- oder XPS-Format exportieren.

    Folgender CodeSchnipsel zeigt wie man eine Excel Datei als PDF exportiert.

     



     

    using System;
    using System.Windows.Forms;
    using System.Collections.Generic;
    using System.Text;
    using Microsoft.Office.Interop.Excel;
    
    namespace ConvertSheetCS
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                string paramSourceBookPath = @"C:\Robert\Test.xlsx";
                string paramExportFilePath = @"C:\Robert\Test.pdf";
                XlFixedFormatType paramExportFormat = XlFixedFormatType.xlTypePDF;
                //XlFixedFormatType paramExportFormat = XlFixedFormatType.xlTypeXPS;
                object paramMissing = Type.Missing;
    
                if (!System.IO.File.Exists(paramSourceBookPath))
                    throw new Exception("Die angegebene Excel Datei ist nicht vorhanden.");
    
                ApplicationClass excelApplication = new ApplicationClass();
    
                Workbook excelWorkBook = null;
    
                XlFixedFormatQuality paramExportQuality = XlFixedFormatQuality.xlQualityStandard;
                bool paramOpenAfterPublish = true;
                bool paramIncludeDocProps = true;
                bool paramIgnorePrintAreas = true;
                object paramFromPage = Type.Missing;
                object paramToPage = Type.Missing;
    
                try
                {
    
                    excelWorkBook = excelApplication.Workbooks.Open(paramSourceBookPath, paramMissing, paramMissing, paramMissing, paramMissing,
                        paramMissing, paramMissing, paramMissing, paramMissing, paramMissing, paramMissing, paramMissing, paramMissing, paramMissing, paramMissing);
    
    
                    if (excelWorkBook != null)
                        excelWorkBook.ExportAsFixedFormat(paramExportFormat, paramExportFilePath, paramExportQuality, paramIncludeDocProps,
                            paramIgnorePrintAreas, paramFromPage, paramToPage, paramOpenAfterPublish, paramMissing);
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
                finally
                {
                    if (excelWorkBook != null)
                    {
                        excelWorkBook.Close(false, paramMissing, paramMissing);
                        excelWorkBook = null;
                    }
    
                    if (excelApplication != null)
                    {
                        excelApplication.Quit();
                        excelApplication = null;
                    }
    
                    GC.Collect();
                    GC.WaitForPendingFinalizers();
                    GC.Collect();
                    GC.WaitForPendingFinalizers();
                }
            }
        }
    }


    Bitte nicht vergessen die Microsoft.Office.Interop.Excel Referenz einzufügen J

    Grüße,

    Robert

    Donnerstag, 17. September 2009 14:25
    Moderator
  • Hallo stumpfomat,

    damit das Codebeispiel oben funktioniert muss das Add-In zum Speichern als PDF / PDF oder XPS installiert sein. Dieses findest du unter http://www.microsoft.com/downloads/details.aspx?FamilyID=f1fc413c-6d89-4f15-991b-63b07ba5f2e5&DisplayLang=de bzw. http://www.microsoft.com/downloads/details.aspx?FamilyID=4d951911-3e7e-4ae6-b059-a2e79ed87041&DisplayLang=de

    Viele Grüße
    Jan
    Montag, 21. September 2009 11:11
  • Hallo stumpfomat,

    Ich kann mich nur an Jans Antwort anschließen und ihm Bedanken für den Hinweis und Dich fragen:

    Haben Dir die Antworten geholfen?

    Grüße,

    Robert

    Montag, 21. September 2009 11:26
    Moderator
  • Hallo stumpfomat,

    Ich gehe davon aus, dass die Antworten Dir weitergeholfen haben.
    Solltest Du noch "Rückfragen" dazu haben, so gib uns bitte Bescheid.

    Grüße,
    Robert

    Montag, 28. September 2009 20:05
    Moderator
  • Hallo alle zusammen,

    sorry ich war in letzter Zeit auch mit C# beschäftigt (anderes Thema allerdings).

    Vielen Dank für die super Hilfen !!!

    Haben mir weitergeholfen.

    Gruß
    Andreas
    Sonntag, 8. November 2009 11:42