2014-10-01 14 views
5

Ho bisogno del mio output di Excel per avere una colonna con il tempo nel formato HH: mm.Come posso convertire un tempo basato su stringa in formato hh: mm in excel usando EPPlus?

Il mio codice è il seguente:

ws.Cells[rowNumber, 11].Style.Numberformat.Format = "hh:mm"; 
string start = row.R_HourStart.ToString("HH:mm:ss"); 
ws.Cells[rowNumber, 11].Value = start; 

Ma quando ho aperto il file in Excel, i display delle cellule "10:10:00".

Quando inserisco del testo nella barra della formula e faccio clic su Invio, il testo della cella viene modificato in "10:10", che è quello che voglio.

Ho anche provato con:

ws.Cells[rowNumber, 11].Style.Numberformat.Format = "hh:mm"; 
string start = row.R_HourStart.ToString("HH:mm:ss"); 
ws.Cells[rowNumber, 11].LoadFromText(start); 

ottengo "10:10" visualizzato nella cella, ma la barra della formula visualizza la data completa di oggi con l'ora 10:10.

Inoltre, ho provato:

ws.Cells[rowNumber, 12].Style.Numberformat.Format = "hh:mm"; 
ws.Cells[rowNumber, 12].Value = row.R_HourEnd; 

Che mostra "10:10" nella cella ma la barra di formula mostra l'intera data e l'ora "22/08/2014 10:10:00".

+0

tenta di impostare un tipo di dati C# DateTime invece di una stringa di celle []. Valore – Fratyx

+0

@Fratyx ho provato che ... – eyalb

+0

Qual è il formato del vostro row.R_HourStart prima di chiamare ToString()? – Chris

risposta

5

Puoi convertire il tuo tempo in un TimeSpan? Questo ha funzionato per me:

var package = new ExcelPackage(new FileInfo("text.xlsx")); 
var sheet = package.Workbook.Worksheets.Add("Sheet 1"); 

var someTime = "10:10:00"; 
var timeSpan = TimeSpan.Parse(someTime); 
sheet.Cells[1, 1].Value = timeSpan; 
sheet.Column(1).Style.Numberformat.Format = "hh:mm"; 

package.Save(); 
+0

Anche se sembra corretto, il valore nella cella cambia in '1-1-1900 10: 10: 00' che non è quello che voglio. –

+0

@HugoDelsing: per me è formattato come "10:10". Stai sovrascrivendo il formato della colonna da qualche altra parte? – Chris

+0

Il formato è corretto, ma se si guarda in alto al valore effettivo, viene visualizzato come '1-1-1900 10: 10'. Se si modifica 'F2' e si preme' enter' per salvare le modifiche (senza modificare nulla), il formato è di nuovo sbagliato. Se dovessi digitare 10:10 in Excel, funzionerebbe diversamente –

0

Ho avuto lo stesso problema ed è stato perché il mio DataTable era tutte le stringhe. La formazione ha funzionato bene dopo aver sostituito i valori con quelli pared.

for (int row = 1; row <= lastRow; row++) 
    { 
    DateTime dt; 
    if (DateTime.TryParse(worksheet.Cells[row, colWithDates]?.Value?.ToString() ?? string.Empty, out dt)) 
    { 
     worksheet.Cells[row, 1].Value = dt; 
     worksheet.Cells[row, 1].Style.Numberformat.Format = "yyyy-mm-dd"; 
    } 

    TimeSpan ts; 
    if (TimeSpan.TryParse(worksheet.Cells[row, columnWithTimes]?.Value?.ToString() ?? string.Empty, out ts)) 
    { 
     worksheet.Cells[row, 5].Value = ts; 
     worksheet.Cells[row, 5].Style.Numberformat.Format = "hh:mm"; 
    } 
} 
Problemi correlati