Page Layout Excel + Erreur exception
Bonjour,
J'essaie de creer un generateur de tableau Excel en C#.
1)Premier probleme:
J'aimerais redimmensionner ma page de facon a ce qu'elle reponde aux criteres suivants:
page A4, mode paysage, zoom a 43%, 7 colonnes de largeur 45, 50 lignes de hauteur 16.
Avec mon code suivant, seulement les colonnes se formattent et non le reste. Je n'arrive pas a savoir pourquoi...
J'ai bien ajoute la reference using Excel = Microsoft.Office.Interop.Excel.
Code:
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();
}
}
}
} |
2) Deuxieme probleme:
Pour plus de lisibilite, j'aimerais creer une fonction qui sert a configurer la feuille et une fonction qui sauvegarde la feuille. Je les ai mis en commentaire dans le code precedent car cela generait une erreur (l.17 et l.37). A l'execution du code, l'erreur suivante apparait:
Citation:
Exception occured while releasing object System.NullReferenceException: reference not set to an instance of an object.
Mais je n'arrive pas a la resoudre. Voila le code des fonctions que j'aimerais mettre dans mon code au-dessus:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| private void ConfigureSheet(Excel.Worksheet xlWorkSheet, Excel.Workbook xlWorkBook)
{
// select the first sheet of the Excel application (3 sheets are available by default)
xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
// Write to the cell [row, column]
xlWorkSheet.Cells[3, 5] = "Bonjour";
}
public void SaveSheet(Excel.Workbook xlWorkBook, object misValue)
{
xlWorkBook.SaveAs(tbPath.Text + @"\ExcelTest.xls", Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);
} |
Si qqn a une idee pour l'une de ces questions, merci par avance!