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:
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 read Excel file and content * * @author tuzongxun123 * */public class Excelde mo1 {public static void main (string[] args) {try {//Gets the file's storage path on the disk, filename 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 the Excel workbook by name Hssfsheet sheet = workbook.getsheet ("Sheet0"); Traverse the removal of rows and columns in 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 a new cell to store the 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