ASF Bugzilla – Attachment 23854 Details for
Bug 47411
xlsx files with date values don't display properly in Mac Excel 2008 (due to different defaults, apparently)
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Help
|
New Account
|
Log In
Remember
[x]
|
Forgot Password
Login:
[x]
TestPoiDate.java - sample code exhibiting behavior
TestPoiDate.java (text/plain), 2.37 KB, created by
Leif Nelson
on 2009-06-23 09:00:10 UTC
(
hide
)
Description:
TestPoiDate.java - sample code exhibiting behavior
Filename:
MIME Type:
Creator:
Leif Nelson
Created:
2009-06-23 09:00:10 UTC
Size:
2.37 KB
patch
obsolete
>package poi.test; > >import java.io.File; >import java.io.FileOutputStream; >import java.io.IOException; >import java.text.DateFormat; >import java.text.SimpleDateFormat; >import java.util.Date; > >import org.apache.poi.ss.usermodel.Cell; >import org.apache.poi.ss.usermodel.CellStyle; >import org.apache.poi.ss.usermodel.DataFormat; >import org.apache.poi.ss.usermodel.Row; >import org.apache.poi.ss.usermodel.Sheet; >import org.apache.poi.xssf.usermodel.XSSFWorkbook; >import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTWorkbookPr; > >public class TestPoiDate { > > public static void main(String[] args) { > try { > TestPoiDate instance = new TestPoiDate(); > instance.generateWorkbook("workbook-noWorkbookPrTag.xlsx", false); > instance.generateWorkbook("workbook-withWorkbookPrTag.xlsx", true); > > } catch (Exception e) { > e.printStackTrace(); > } > } > > private void generateWorkbook(String workbookName, boolean addWorkbookPr) throws IOException { > > XSSFWorkbook workbook = new XSSFWorkbook(); > > if (addWorkbookPr) { > if (workbook.getCTWorkbook().getWorkbookPr() == null) { > // this code causes a <workbookpr> element to be written to the > // workbook.xml file > // the presence of this line causes all workbooks to go into > // date1900 mode which all the > // dates are written. If this mode isn't set, then Mac Excel > // 2008 defaults to date1904 mode > // whereas Windows Excel 2007 the default is date1900 mode > CTWorkbookPr workbookPr = CTWorkbookPr.Factory.newInstance(); > workbookPr.setDate1904(false); > workbook.getCTWorkbook().setWorkbookPr(workbookPr); > } > } > > Sheet s = workbook.createSheet(); > Row r1 = s.createRow(0); > Cell c1 = r1.createCell(0); > c1.setCellValue("Date Cell:"); > > CellStyle cs = workbook.createCellStyle(); > DataFormat dataFormat = workbook.createDataFormat(); > cs.setDataFormat(dataFormat.getFormat("mm/dd/yy")); > DateFormat df = new SimpleDateFormat("MM/dd/yy"); > > Date now = new Date(); > Cell c2 = r1.createCell(1); > c2.setCellValue(now); > c2.setCellStyle(cs); > > Row r2 = s.createRow(1); > > Cell c3 = r2.createCell(0); > c3.setCellValue("String Cell:"); > Cell c4 = r2.createCell(1); > c4.setCellValue(df.format(now)); > > String filename = workbookName; > File file = new File(filename); > if (file.exists()) { > file.delete(); > } > > FileOutputStream out = new FileOutputStream(filename); > workbook.write(out); > out.close(); > > } > >}
You cannot view the attachment while viewing its details because your browser does not support IFRAMEs.
View the attachment on a separate page
.
View Attachment As Raw
Actions:
View
Attachments on
bug 47411
:
23853
| 23854 |
23855
|
23856
|
23857
|
23858