1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
| namespace Generateur_TabExcel_v2
{
public partial class Form1 : Form
{
public void CreateSheet ()
{
Excel.Application xlApp = null;
Excel.Workbook xlWorkBook = null;
Excel.Worksheet xlWorkSheet = null;
object misValue = Missing.Value;
xlApp = new Excel.Application();
xlWorkBook = xlApp.Workbooks.Add(misValue);
// CONFIGURATION SHEET
//ConfigureSheet(xlWorkSheet, xlWorkBook);
xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
// Change the width of the column to 45
((Excel.Range)xlWorkSheet.Columns["A", Type.Missing]).ColumnWidth = 45;
((Excel.Range)xlWorkSheet.Columns["B", Type.Missing]).ColumnWidth = 45;
((Excel.Range)xlWorkSheet.Columns["C", Type.Missing]).ColumnWidth = 45;
((Excel.Range)xlWorkSheet.Columns["D", Type.Missing]).ColumnWidth = 45;
((Excel.Range)xlWorkSheet.Columns["E", Type.Missing]).ColumnWidth = 45;
((Excel.Range)xlWorkSheet.Columns["F", Type.Missing]).ColumnWidth = 45;
((Excel.Range)xlWorkSheet.Columns["G", Type.Missing]).ColumnWidth = 45;
// Set the paper size
xlWorkSheet.PageSetup.PaperSize = Excel.XlPaperSize.xlPaperA4;
// Change the page orientation
xlWorkSheet.PageSetup.Orientation = Excel.XlPageOrientation.xlLandscape;
// Change the scale of the page to 43%
xlWorkSheet.PageSetup.Zoom = 43;
// SAVE SHEET
//SaveSheet(xlWorkBook, misValue);
xlWorkBook.SaveAs(tbPath.Text + @"\ExcelTest.xls", Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);
xlWorkBook.Close(true, misValue, misValue);
xlApp.Quit();
releaseObject(xlWorkSheet);
releaseObject(xlWorkBook);
releaseObject(xlApp);
}
private void releaseObject(object obj)
{
try
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
obj = null;
}
catch (Exception ex)
{
obj = null;
MessageBox.Show("Exception Occured while releasing object " + ex.ToString());
}
finally
{
GC.Collect();
}
}
}
} |
Partager