2016-10-28 2 views
2

Bitte sehen Sie sich das folgende Code-Snippet an. Ich öffne einfach die Excel-Datei myfile.xlsx und ich füge Zeilen von einem Objekt des Typs List<Account> hinzu (wo mein Account Objekt nur Date, Account und Amount Eigenschaften hat), und speichern Sie die Datei mit dem Namen myoutputfile.xlsx. Ich möchte die Zellen, in denen ich die Daten schreibe, um ein Datumsformat zu haben, und die Zellen, in denen ich habe, um ein numerisches Format zu haben. Wenn ich jedoch den folgenden Code ausprobiere, werden alle Zellen mit dem Format #.00 (Datumsangaben) formatiert. Ich habe alles versucht, kann mir bitte jemand sagen, was los ist? Ich benutze NPOI.NPOI formatiert alle Zellen auf die gleiche Weise

XSSFWorkbook wb; 
    var fileName = "C:/tmp/myfile.xlsx"; 
    var outputFileName = "C:/tmp/myoutputfile.xlsx"; 
    using (var file = new FileStream(fileName, FileMode.Open, FileAccess.ReadWrite)) 
    { 
     wb = new XSSFWorkbook(file); 
    } 

    XSSFSheet sheet = (XSSFSheet) wb.GetSheetAt(0); 
    for (int i = 0; i < accountRecs.Count(); ++i) { 
     var rec = accountRecs[i]; 
     var row = sheet.CreateRow(i); 
     var dateCell = row.CreateCell(3); 
     dateCell.SetCellValue(rec.Date); 
     dateCell.CellStyle.DataFormat = wb.CreateDataFormat().GetFormat("dd/MM/yyyy"); 
     var accountCell = row.CreateCell(4); 
     accountCell.SetCellValue(rec.Account); 
     var totalValueCell = row.CreateCell(16); 
     totalValueCell.SetCellValue(rec.Amount); 
     totalValueCell.CellStyle.DataFormat = wb.CreateDataFormat().GetFormat("#.00"); 
    } 
    using (var file = new FileStream(outputFileName, FileMode.Create, FileAccess.Write)) 
    { 
     wb.Write(file); 
     file.Close(); 
    } 
+0

möglicherweise im Zusammenhang mit http://stackoverflow.com/a/3603750/619252. Verwenden Sie das integrierte Format für "TT/MM/JJJJ". –

Antwort

2

Hier ist, warum es nicht funktioniert: Die Zellen Sie erstellen standardmäßig einen Verweis auf das gleiche Objekt CellStyle teilen. Innerhalb der Schleife setzen Sie die DataFormat für diese Stilinstanz auf "dd/MM/yyyy", und setzen Sie später dieselbe DataFormat auf "#.00". Der letzte gewinnt, also werden alle Ihre numerischen Zellen (ein Datum wird in Excel als numerischer Wert betrachtet) als "#.00" formatiert.

Was Sie tun müssen, ist separate Zellstile für Ihre Datenzellen und Ihre Menge Zellen zu erstellen, setzen Sie die DataFormats auf diese Stile, dann legen Sie die CellStyle -Eigenschaft für jede erstellte Zelle auf den entsprechenden Stil.

Probieren Sie es wie folgt aus:

IDataFormat format = wb.CreateDataFormat(); 

    ICellStyle dateStyle = wb.CreateCellStyle(); 
    dateStyle.DataFormat = format.GetFormat("dd/MM/yyyy"); 

    ICellStyle amountStyle = wb.CreateCellStyle(); 
    amountStyle.DataFormat = format.GetFormat("#.00"); 

    XSSFSheet sheet = (XSSFSheet)wb.GetSheetAt(0); 
    for (int i = 0; i < accountRecs.Count(); ++i) 
    { 
     var rec = accountRecs[i]; 
     var row = sheet.CreateRow(i); 
     var dateCell = row.CreateCell(3); 
     dateCell.SetCellValue(rec.Date); 
     dateCell.CellStyle = dateStyle; 
     var accountCell = row.CreateCell(4); 
     accountCell.SetCellValue(rec.Account); 
     var totalValueCell = row.CreateCell(16); 
     totalValueCell.SetCellValue(rec.Amount); 
     totalValueCell.CellStyle = amountStyle; 
    } 
+0

Vielen Dank Brian. Du hast mir die Erklärung und die Antwort gegeben. Es funktioniert. Habe eine gute – edd

+0

Ich bin froh, dass ich helfen konnte. –

Verwandte Themen