2016-09-15 2 views
0

I Apache POI 3.11 Version zum Lesen verwenden und in JavaGet Format von Datum für Datumsspalte in Excel

In meinem Input Excel-Datei eine Excel-Datei zu schreiben, habe ich eine Spalte, das Datum enthält. Das Datum kann ein beliebiges Format haben, wie zB TT/MM/JJJJ hh: mm oder JJ/MM/TT usw. Während ich die Datei lese, kann ich ein Datum in jedem Format bekommen.

enter image description here

Wenn ich die neue Datei als Ausgabe Excel erschaffe, möchte ich Datumsspalte schreiben. Aber ich möchte das gleiche Format des Datums verwenden, das in der Eingabe-Excel-Datei vorhanden war.

Gibt es eine Möglichkeit, durch die ich in der Eingabe das Datumsformat heraus kann Excel-Datei (Above Screenshot Sie das Datum in mm/tt/hh war sehen: mm: ss PM-Format)

Ich möchte nicht ***getCellStyle*** Funktion verwenden, weil es mir den vollständigen Stil der Zelle einschließlich der Schriftfarbe, Hintergrundfarbe usw. zurückgibt. Ich möchte nur das Datum Format Informationen einer Zelle, die in der Eingabe Excel-Datei vorhanden war.

Way Ich versuche:

CellStyle outputStyle = wb.createCellStyle(); 
Font textFont = wb.createFont(); 
textFont.setFontName("Arial"); 
textFont.setFontHeightInPoints((short) 10); 
textFont.setColor(IndexedColors.BLACK.getIndex()); 
.setFont(textFont); 

if (DateUtil.isCellDateFormatted(icell)) { 
outputStyle.setDataFormat(icell.getCellStyle().getDataFormat()); 
ocell.setCellValue(icell.getDateCellValue()); 
} 

ocell.setCellStyle(outputStyle); 

Es wird den Wert von Datum in der Zelle als Zahl zu schreiben.

Antwort

1

Sie müssen das ganze CellStyle Objekt durch getCellStyle, die Zeichenfolge Datenformat der Zelle Stil von getDataFormatString mit Hilfe von CreationHelper kehrte erst wieder nicht verwenden:

// Create the cell style in the new WorkBook 
CreationHelper createHelper = outputWB.getCreationHelper(); 
CellStyle outputStyle = outputWB.createCellStyle(); 

// Set the data format using the data format string 
outputStyle.setDataFormat(createHelper.createDataFormat() 
     .getFormat(cell.getCellStyle().getDataFormatString())); 
// Set the created cell style to the new cell 
newCell.setCellStyle(outputStyle); 

Variable cell ist die Cell lesen aus der aktuellen Datei und Variable newCell ist die Cell in die neue Datei geschrieben werden.

+0

Es hat nicht funktioniert. Das Datum wird immer noch als numerischer Wert geschrieben. –

+0

Schreiben Sie den Wert als "Date"? Könnten Sie in Ihre Frage zumindest schreiben, wie Sie schreiben und wie Sie die Datumszellen schreiben? – DVarga

+0

Ja Ich schreibe als Date-Typ von Wert. CellStyle outputStyle = wb.createCellStyle(); Schriftart textFont = wb.createFont(); textFont.setFontName ("Arial"); textFont.setFontHeightInPoints ((kurz) 10); textFont.setColor (IndexedColors.BLACK.getIndex()); .setFont (TextFont); \t \t \t \t if (DateUtil.isCellDateFormatted (Icell)) { outputStyle.setDataFormat (icell.getCellStyle() getDataFormat().); ocell.setCellValue (icell.getDateCellValue()); } ocell.setCellStyle (outputStyle); –

Verwandte Themen