Benutzer mit den meisten Antworten
Drucken über C# von definierten Excel Dateien bzw. Worksheets

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(); } } }
- Bearbeitet Robert BreitenhoferModerator Montag, 14. September 2009 19:40 Code Formatierung
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
- Als Antwort vorgeschlagen Robert BreitenhoferModerator Montag, 21. September 2009 11:27
- Als Antwort markiert Robert BreitenhoferModerator Montag, 28. September 2009 20:05
-
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- Als Antwort vorgeschlagen Robert BreitenhoferModerator Montag, 21. September 2009 11:27
- Als Antwort markiert Robert BreitenhoferModerator Montag, 28. September 2009 20:05
Alle 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
- Als Antwort vorgeschlagen Robert BreitenhoferModerator Montag, 21. September 2009 11:27
- Als Antwort markiert Robert BreitenhoferModerator Montag, 28. September 2009 20:05
-
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- Als Antwort vorgeschlagen Robert BreitenhoferModerator Montag, 21. September 2009 11:27
- Als Antwort markiert Robert BreitenhoferModerator Montag, 28. September 2009 20:05