Java programs convert data in scientific notation in Excel to date type

Source: Internet
Author: User

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 
  1. Package excelDemo1;
  2. Import Java.io.BufferedInputStream;
  3. Import Java.io.File;
  4. Import Java.io.FileInputStream;
  5. Import Java.io.FileOutputStream;
  6. Import Java.io.OutputStream;
  7. Import Java.text.DecimalFormat;
  8. Import Java.util.Date;
  9. Import Org.apache.poi.hssf.usermodel.HSSFCell;
  10. Import Org.apache.poi.hssf.usermodel.HSSFRow;
  11. Import Org.apache.poi.hssf.usermodel.HSSFSheet;
  12. Import Org.apache.poi.hssf.usermodel.HSSFWorkbook;
  13. Import Org.apache.poi.poifs.filesystem.POIFSFileSystem;
  14. /**
  15. * Java reads Excel files and contents
  16. *
  17. * @author tuzongxun123
  18. *
  19. */
  20. Public class ExcelDemo1 {
  21. public static void Main (string[] args) {
  22. try {
  23. //Get file storage path on disk
  24. File File = new file ("C:" + file.separator + "Users"
  25. + File.separator + "tuzongxun123" + file.separator
  26. + "Desktop" + file.separator + "Log.xls");
  27. //Read file input stream
  28. FileInputStream FileInputStream = new FileInputStream (file);
  29. Bufferedinputstream Bufferedinputstream = new Bufferedinputstream (
  30. FileInputStream);
  31. Poifsfilesystem excelfile = new Poifsfilesystem (Bufferedinputstream);
  32. Hssfworkbook workbook = new Hssfworkbook (Excelfile);
  33. //Get an Excel workbook by name
  34. Hssfsheet sheet = workbook.getsheet ("Sheet0");
  35. //Traverse to remove rows and columns from the workbook
  36. For (int i = 1;; i++) {
  37. Hssfrow row = Sheet.getrow (i);
  38. if (row! = null) {
  39. //Get scientific notation for this column of cells
  40. Hssfcell cell = Row.getcell (6);
  41. //Format scientific notation data format
  42. DecimalFormat df = new DecimalFormat ("0");
  43. String value = Df.format (Cell.getnumericcellvalue ());
  44. //Time conversion
  45. Long long1 = Long.parselong (value);
  46. Date date = new Date (LONG1);
  47. //Create new cells to store converted data
  48. Hssfcell cell2 = Row.createcell (9);
  49. Cell2.setcellvalue (date);
  50. } Else {
  51. Break ;
  52. }
  53. }
  54. //write the converted data to a file
  55. OutputStream outputstream = new FileOutputStream (file);
  56. Workbook.write (OutputStream);
  57. Outputstream.close ();
  58. } catch (Exception e) {
  59. E.printstacktrace ();
  60. }
  61. }
  62. }


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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.