Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to solve the format problems such as importing pure numbers into poi by java

2025-03-13 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

Editor to share with you java how to solve poi import pure numbers and other format problems, I believe that most people do not understand, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to understand it!

Problems such as importing pure numbers into poi

When exporting excel with poi, if the cell sets a pure number, once the input data is too large, it will automatically be displayed as scientific notation, resulting in errors in the imported data. The solution: after obtaining the exported file in the background, the forced conversion of cell attributes can be perfectly solved, and it is also suitable for data import exceptions caused by other cell formats.

Cell cellCode = r.getCell (1); cellCode.setCellType (CellType.STRING); info.setCode (r.getCell (1). GetStringCellValue ()); poi access to Cell content: digital formatting

Today, I received a request from the business side that the amount imported by excel has been rounded up.

Then take a look at the code: at first, it was dazzling, and I really couldn't watch it. But think about it, as a programmer, you can't refuse to serve the people. As a result, debug took a moment. I'm really lazy. I don't want to chase the code directly. Debug went.

Reason

Found the specific reason:

If (xssfRow.getCellType () = = Cell.CELL_TYPE_NUMERIC) {DecimalFormat format = new DecimalFormat ("#"); double num = xssfRow.getNumericCellValue (); String res = format.format (num); / /. }

In the above code, the number is formatted as an integer. Of course, there will be no problem if you get the value directly.

As follows:

If (xssfRow.getCellType () = = Cell.CELL_TYPE_NUMERIC) {DecimalFormat format = new DecimalFormat ("#"); double num = xssfRow.getNumericCellValue (); String res = format.format (num); / / the values of num and res are similar. For example, num is 50.00: num is 50.00; 123.23, num is 123.23, res is 123.23 System.err.println (num + "- -" + res); / /... } DecimalFormat

DecimalFormat is a concrete subclass of NumberFormat that is used to format decimal numbers. Help you format numbers as quickly as you need them. DecimalFormat contains a pattern and a set of symbols.

The DecimalFormat class mainly relies on # and 0 placeholders to specify the length of the number. Like the "#. 000" symbol. This pattern means that there are four digits before the decimal point, empty if not enough, and three digits after the decimal point, which is not made up by 0.

Symbolic meaning:

0 a number

# A number, excluding 0

. A placeholder for the separator of a decimal

The placeholder of the component separator

-the default negative prefix.

% multiplied by 100 and displayed as a percentage

Example:

Public static void main (String [] args) {double pi=3.1415927;// pi / / take one integer System.out.println (new DecimalFormat ("0") .format (pi); / / 3 / / take one integer and two decimal places System.out.println (new DecimalFormat ("0.00") .format (pi)) / / 3.14 / / take two integers and three decimal places, and the integer deficiency is filled with 0. System.out.println (new DecimalFormat ("00.000") .format (pi); / / 03.142 / / take all integer portions of System.out.println (new DecimalFormat ("#") .format (pi)); / / 3 / / count as a percentage and take two decimal places System.out.println (new DecimalFormat ("#%") .format (pi)); / 314.16% long caches 299792458 / / Speed of light / / display as scientific counting, and take five decimal places System.out.println (new DecimalFormat ("# # E0") .format (c)); / / 299.792 million / / display scientific counting as two-digit integers, and take four decimal places System.out.println (new DecimalFormat ("00.####E0") .format (c)) / / 299.792 million / every three digits are separated by commas. System.out.println (new DecimalFormat (", #") .format (c); / 299792458 System.out.println (new DecimalFormat ("- #") .format (c)); / / 299792458 System.out.println (new DecimalFormat ("#?") .format (c)) / / 299792458 / / embed the format into the text System.out.println (new DecimalFormat ("the speed of light is # meters per second") .format (c); / / the speed of light is 299792458 meters per second} is all the content of the article "how to solve the format problem of poi importing pure numbers into poi". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report