Mit C# Excel-Zellen formatieren
-
Hallo,
ich mache mit C# einen Excel-Export.
Das klappt soweit auch prima.Nun habe ich noch das Problem, wenn Nummern bsw. 000321 in Excel geschrieben werden, die ersten 3 Stellen wegfallen.
Wie kann ich also über C# meine Zelle(n) als "Text" formatieren?
Meine sonstigen Sachen wie bsw. Rahmen etc. habe ich so gelöst:
Range rg9 = oSheet.get_Range("A2", strZeilen); rg9.Borders[Excel.XlBordersIndex.xlInsideHorizontal].ColorIndex = Excel.XlColorIndex.xlColorIndexAutomatic; rg9.Borders[Excel.XlBordersIndex.xlInsideHorizontal].LineStyle = Excel.XlLineStyle.xlContinuous; rg9.Borders[Excel.XlBordersIndex.xlInsideHorizontal].Weight = Excel.XlBorderWeight.xlThin;
Würde mich sehr über Eure Hilfe freuen - danke!
-
Wie setzt du den Text?
Ich habe gerade mal in meine ExcelExporter klasse rein gesehen, dort habe ich es so:
public void SetData(int row, int column, string text) { _worksheet.Cells[row, column] = text; }
Anbei meine Methoden aus dem Exporter, eventuell enthalten diese nützliche Informationen
private Excel.Application _application; private Excel.Workbook _workbook; private Excel.Worksheet _worksheet; ... _application = new Excel.Application(); CultureInfo oldCulture = CultureInfo.CurrentCulture; CultureInfo excelCulture = new CultureInfo(_application.LanguageSettings.get_LanguageID(Microsoft.Office.Core.MsoAppLanguageID.msoLanguageIDUI)); Thread.CurrentThread.CurrentUICulture = excelCulture; Thread.CurrentThread.CurrentCulture = excelCulture; _workbook = _application.Workbooks.Add(1); _worksheet = (Excel.Worksheet)_workbook.Sheets[1]; Visible = false; ... public string ColumnIndexToName(int index) { string columnName = ""; int rest = 0; if (index > 26) { do { index = Math.DivRem(index, 26, out rest); if (rest == 0) { index -= 1; rest = 26; } columnName = (char)(rest + 64) + columnName; } while (index > 26); } columnName = (char)(index + 64) + columnName; return columnName; } public void SetData(int row, int column, string text) { _worksheet.Cells[row, column] = text; } public string GetData(int row, int column) { return _worksheet.Cells[row, column] as string; } public void SetCellBorder(string cell1, string cell2, Excel.XlBordersIndex position, Excel.XlLineStyle style, Excel.XlBorderWeight weight, System.Drawing.Color color) { Excel.Range workSheetRange = _worksheet.get_Range(cell1, cell2); workSheetRange.Borders[position].Weight = weight; workSheetRange.Borders[position].LineStyle = style; workSheetRange.Borders[position].Color = color.ToArgb(); } public void SetCellColor(string cell1, string cell2, System.Drawing.Color color) { Excel.Range workSheetRange = _worksheet.get_Range(cell1, cell2); workSheetRange.Interior.Color = color.ToArgb(); } public void SetCellFont(string cell1, string cell2, bool bold, bool italic, bool underline, System.Drawing.Color color) { Excel.Range workSheetRange = _worksheet.get_Range(cell1, cell2); workSheetRange.Font.Bold = bold; workSheetRange.Font.Italic = italic; workSheetRange.Font.Color = color.ToArgb(); workSheetRange.Font.Underline = underline; } public void SetColumnWidth(int column, double width) { ((Excel.Range)_worksheet.Columns[column, Type.Missing]).ColumnWidth = width; } public void SetRowHeight(int row, double height) { ((Excel.Range)_worksheet.Rows[row, Type.Missing]).RowHeight = height; } public void Save(string fileName) { _workbook.SaveAs(fileName, Excel.XlFileFormat.xlExcel8, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Excel.XlSaveAsAccessMode.xlNoChange, Excel.XlSaveConflictResolution.xlUserResolution, Type.Missing, Type.Missing, Type.Missing, Type.Missing); } public void Close() { _application.Quit(); }
-
Den Text selber setzte ich wie Du in Deinem Beispiel:
... if (oWB.Worksheets.Count > 0) { oSheet = (Excel.Worksheet)oWB.Sheets[1]; for (int j = 1; j < dgvBilanz.Columns.Count; j++) { for (int i = 0; i < dgvBilanz.Rows.Count - 1; i++) { oSheet.Cells[z, s] = dgvBilanz[j, i].Value.ToString(); z++; } s++; z = 4; } } ...
Dabei schreibe ich eben auch Zahlenwerte in Excel. Diese sollen aber als Text formatiert sein. In Excel: Zellen formatieren -> Text
Wobei das ganze wohl vor dem Export statt finden müsste ...
-
Versuchs mal mit:
workSheetRange.NumberFormat = "@";
-
Super! Hat funktioniert, vielen Dank!
-
Hallo,
ich nehme einfach mal diesen älteren Thread, da ich auch ein Problem mit dem Formatieren von Excel-Zellen habe.
Ich möchte, dass meine Spalten als Währung (€) angezeigt werden:
rg3.NumberFormat = "#,## €";
So wird allerdings bei einem Betrag von 0,83€ die führende 0 weggelassen (,83€) bzw. wenn es 7,50€ macht steht nur 7,5 € in der Zelle.
Wie muss ich die Zellen entsprechend formatieren, um auch die 0-en angezeigt zu bekommen?
Dankeschön!
-
Vermutlich mit "0,#0 €"