我试图将我的数字(双精度型)移动到单元格的右侧。我从datagridview中获取所有数据并将其放在我的excel表中
一个excel表包括5列。第一个是字符串,其余的是int和double类型的数字。 如果我的单元格中有一个带有昏迷的数字,则数字在右侧,而没有昏迷的数字在左侧。 我读过我可以使用类似的东西-> myRange.FormatNumber。 但是我现在不知道如何使用它...
try {
string path = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
saveFileDialog1.InitialDirectory = path;
saveFileDialog1.Title = "Als Excel Datei speichern";
saveFileDialog1.FileName = "";
saveFileDialog1.Filter = "Excel Files(2013)|*.xlsx|Excel Files(2003)|*.xls|Excel Files(2007)|*.xlsx";
if (saveFileDialog1.ShowDialog() != DialogResult.Cancel)
{
Cursor.Current = Cursors.WaitCursor;
Microsoft.Office.Interop.Excel.Application ExcelApp = new Microsoft.Office.Interop.Excel.Application();
ExcelApp.Application.Workbooks.Add(Type.Missing);
object[,] arr = new object[maximaleZeilenAnzahl, maximaleSpaltenAnzahl]; //Array erstellen
//Überschrift Tabelle Einzeln
for (int columnIndex = 0; columnIndex < dgvEinzeln.Columns.Count; columnIndex++)
{
arr[aktuelleExcelZeile, columnIndex] = dgvEinzeln.Columns[columnIndex].HeaderText;
}
aktuelleExcelZeile += 1;
//Tabelle Einzeln
for (int rowIndex = 0; rowIndex < dgvEinzeln.Rows.Count; rowIndex++)
{
for (int columnIndex = 0; columnIndex < dgvEinzeln.Columns.Count; columnIndex++)
{
arr[aktuelleExcelZeile, columnIndex] = dgvEinzeln.Rows[rowIndex].Cells[columnIndex].Value.ToString();
}
aktuelleExcelZeile++;
}
aktuelleExcelZeile += 5;
//Überschrift Tabelle Summe
for (int columnIndex = 0; columnIndex < dgvSumme.Columns.Count; columnIndex++)
{
arr[aktuelleExcelZeile, columnIndex] = dgvSumme.Columns[columnIndex].HeaderText;
}
aktuelleExcelZeile += 1;
//Tabelle Summe
for (int rowIndex = 0; rowIndex < dgvSumme.Rows.Count; rowIndex++)
{
for (int columnIndex = 0; columnIndex < dgvSumme.Columns.Count; columnIndex++)
{
arr[aktuelleExcelZeile, columnIndex] = dgvSumme.Rows[rowIndex].Cells[columnIndex].Value.ToString();
}
aktuelleExcelZeile++;
}
Range c1 = (Range)ExcelApp.Cells[1, 1];
Range c2 = (Range)ExcelApp.Cells[aktuelleExcelZeile, maximaleSpaltenAnzahl];
Range range = ExcelApp.get_Range(c1, c2);
range.Value = arr;
ExcelApp.ActiveWorkbook.SaveCopyAs(saveFileDialog1.FileName.ToString());
ExcelApp.ActiveWorkbook.Saved = true;
ExcelApp.Quit();
string filename = Path.GetFullPath(saveFileDialog1.FileName);
System.Diagnostics.Process.Start(filename);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
throw;
}
finally
{
Cursor.Current = Cursors.Default;
}
答案 0 :(得分:0)
请尝试这个
ExcelApp.Cells[rownumbwe, columnNumber].HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignRight;