/ / C # Gridview nach Excel mit Zahlenformatierung und Tabellenzeile - c #, Excel, Formatierung

C # Gridview zu Excel mit Zahlenformatierung und Tabellenzeile - c #, Excel, Formatierung

Hallo, ich möchte meine Daten in Gridview nach Excel importieren.Meine Daten in Gridview sind "00, 02, 04", aber in Excel ändern sich meine Daten in "0, 2, 4" , für exp habe ich 160 Zeilen Daten, ich möchte in Excel auf 3 Spalten aufgeteilt werden, 1 Spalte hat 40 Zeilen. Dies ist mein Code:

private void exToExcel()
{
if (RekapdataGridView.Rows.Count > 0)
{
Microsoft.Office.Interop.Excel.Application XcelApp = new Microsoft.Office.Interop.Excel.Application();
XcelApp.Application.Workbooks.Add(Type.Missing);
//XcelApp.Cells[5,2].NumberFormat = "00";
XcelApp.Cells[1, 1] = "Tanggal";
XcelApp.Cells[1, 2] = labelTglRekap.Text;
XcelApp.Cells[2, 1] = "Kode Pemain";
XcelApp.Cells[2, 2] = labelKodePemain.Text;
XcelApp.Cells[4, 1] = "No";
int x = RekapdataGridView.RowCount;
for (int y = 1; y <= RekapdataGridView.RowCount; y++)
{
XcelApp.Cells[4+y, 1] = y;
}

for (int i = 2; i < RekapdataGridView.Columns.Count + 2; i++)
{
XcelApp.Cells[4, i] = RekapdataGridView.Columns[i - 2].HeaderText;
}

for (int i = 0; i < RekapdataGridView.Rows.Count; i++)
{
for (int j = 0; j < RekapdataGridView.Columns.Count; j++)
{
XcelApp.Cells[i + 5, j + 2] =string.Format("{0:00}", RekapdataGridView.Rows[i].Cells[j].Value);
}
}
XcelApp.Columns.AutoFit();

XcelApp.Visible = true;
}
}

Das sind meine Daten in Excel:

Bildbeschreibung hier eingeben

Ich möchte, dass meine Daten wie dieses Bild angezeigt werden können: Bildbeschreibung hier eingeben

Sorry, wenn mein Englisch schlecht ist, hoffe ich, dass mir jemand helfen kann.

Antworten:

0 für die Antwort № 1

Du hättest es fast geschafft

XcelApp.Cells[5,2].NumberFormat = "00";

Ändern Sie es einfach in

XcelApp.Cells[5,2].NumberFormat = "@";

Dadurch wird das Zellenformat statt der Zahl "Text". Excel versucht immer, Zahlen aus meiner Erfahrung heraus zu vereinfachen.

Vergiss nicht, es für ALLE Zellen zu tun, in die du schreibst.

Hoffe das hilft!