Some data is exported from the MongoDB database for some reason today, and in order to send it to other people more visually, we use Mongovue's export as Excel function. However, after the export of a problem, there is a column of storage time, stored in a long type of milliseconds, after the export has automatically become the scientific notation. And not to distinguish the actual time from the scientific notation, even the original millisecond value is not easy to discern, but these long values can not be converted directly into the date type in Excel, so I would like to use POI to convert these millisecond values to date and then write the original file.
the scientific notation for Excel exported from MongoDB such as:
The Java program is as follows:
[Java]View PlainCopy
- Package excelDemo1;
- Import Java.io.BufferedInputStream;
- Import Java.io.File;
- Import Java.io.FileInputStream;
- Import Java.io.FileOutputStream;
- Import Java.io.OutputStream;
- Import Java.text.DecimalFormat;
- Import Java.util.Date;
- Import Org.apache.poi.hssf.usermodel.HSSFCell;
- Import Org.apache.poi.hssf.usermodel.HSSFRow;
- Import Org.apache.poi.hssf.usermodel.HSSFSheet;
- Import Org.apache.poi.hssf.usermodel.HSSFWorkbook;
- Import Org.apache.poi.poifs.filesystem.POIFSFileSystem;
- /**
- * Java reads Excel files and contents
- *
- * @author tuzongxun123
- *
- */
- Public class ExcelDemo1 {
- public static void Main (string[] args) {
- try {
- //Get file storage path on disk
- File File = new file ("C:" + file.separator + "Users"
- + File.separator + "tuzongxun123" + file.separator
- + "Desktop" + file.separator + "Log.xls");
- //Read file input stream
- FileInputStream FileInputStream = new FileInputStream (file);
- Bufferedinputstream Bufferedinputstream = new Bufferedinputstream (
- FileInputStream);
- Poifsfilesystem excelfile = new Poifsfilesystem (Bufferedinputstream);
- Hssfworkbook workbook = new Hssfworkbook (Excelfile);
- //Get an Excel workbook by name
- Hssfsheet sheet = workbook.getsheet ("Sheet0");
- //Traverse to remove rows and columns from the workbook
- For (int i = 1;; i++) {
- Hssfrow row = Sheet.getrow (i);
- if (row! = null) {
- //Get scientific notation for this column of cells
- Hssfcell cell = Row.getcell (6);
- //Format scientific notation data format
- DecimalFormat df = new DecimalFormat ("0");
- String value = Df.format (Cell.getnumericcellvalue ());
- //Time conversion
- Long long1 = Long.parselong (value);
- Date date = new Date (LONG1);
- //Create new cells to store converted data
- Hssfcell cell2 = Row.createcell (9);
- Cell2.setcellvalue (date);
- } Else {
- Break ;
- }
- }
- //write the converted data to a file
- OutputStream outputstream = new FileOutputStream (file);
- Workbook.write (OutputStream);
- Outputstream.close ();
- } catch (Exception e) {
- E.printstacktrace ();
- }
- }
- }
After you run the program, add a column of data in Excel such as:
When you format this column cell as a time type in Excel, you see that the data becomes the correct time format, such as:
Java programs convert data in scientific notation in Excel to date type