How to convert exponents in a csv file from Java - java

I am printing some data into a CSV file using Apache Commns CSV. One of the fields contains 15 digit number and is of type String. This field prints as exponential number in CSV instead of a complete number. I know Excel does that but is there a way in java to print it as a complete number.
I am not doing anything special. Initially I thought that Commons CSV will take care of it.
public void createCSV(){
inputStream = new FileInputStream("fileName");
fileWriter = new FileWriter("fileName");
csvFileFormat = CSVFormat.Excel.withHeader("header1", "header2");
csvFilePrinter = new CSVPrinter(fileWriter, csvFileFormat);
for(List<UiIntegrationDTO dto: myList>){
String csvData = dto.getPolicyNumber();
csvFilePrinter.PrintRecord(csvData);
}
}

Prepend apostrophe
As far as I understand from the discussion in comments, it is a question about Excel interpretation of CSV file, but the file itself contains all necessary data.
I think, csvFilePrinter.PrintRecord("'" + csvData); should help. Apostrophe requires Excel to interpret a field as a string, not as a number.

Related

while Parsing TXT to CSV using JAVA long numbers are getting converted to exponential

I have big txt file which needs to converted to CSV format using JAVA ,
I am parsing the file but file is having some long nos in a column which is getting converted to exponential .
like :89148000006119921953 ->8.9148E
I need to parse the txt to csv in such a way so what csv opens with excel doesn't not convert to exponentials values
Please find the below code :
public static String converter(String filename) throws Exception {
FileWriter writer = null;
if (filename.toString().endsWith("TXT")) {
File file = new File("C:\\convertertool\\inputFiles\\" + filename + "");
Scanner scan = new Scanner(file);
filename = filename.replace("TXT", "CSV");
File file2 = new File("C:\\convertertool\\ParsedFiles\\" + filename + "");
file.createNewFile();
writer = new FileWriter(file2);
while (scan.hasNext()) {
String csv = scan.nextLine().replace("|", ",");
System.out.println(csv);
writer.append(csv);
writer.append("\n");
writer.flush();
}
}
return filename;
}
Double.parseDouble("891413E")//or BigDecimal.parse...
Can help you
The assumptions are:
(1) You want to open the CSV file (e.g. test.csv) in Excel by double-clicking on the file, so that the data is displayed as a sequence of digits (instead of using scientific notation).
(2) You do not want to perform any manual formatting on the Excel file, after it has been opened (so, for example you do not want to have to change the Excel cell formatting).
(3) You do not want to use the Excel manual import wizard.
The only way I know of doing this is to write an Excel formula to the CSV file, instead of writing the actual, unadulterated numeric value. You can use any Excel formula which converts the number to a string.
For example, if the numeric value is this:
89148000006119921953
then the CSV file needs to contain the following (I added field one just for this demo):
field one,"=TEXT(89148000006119921953,""#"")"
You can try this for yourself by pasting the above into a test.csv file, and seeing what happens when you double-click on it.
The result in Excel is this:
In this case the Excel formula (if created directly in Excel) would be this:
=TEXT(1234567890123450000,"#")
So in the CSV file, we have to surround the field in double quotes, and we have to escape the double quotes inside the formula by doubling them.
There are some disadvantages to doing this:
(a) The text file no longer contains the pure, unchanged data. It contains an Excel formula instead. So it is more-or-less useless outside of Excel.
(b) The value in Excel is a text formula not a number (so you can't perform arithmetic on it).
(c) The Excel column widths will not auto-resize, so if there is any data in cell C1, then cell B1's display will be truncated (but the full text will still be there).
(d) Now you almost certainly do need a proper Java CSV parsing library to make sure you generate valid CSV data.
Final thought: If the end result needs to be viewed in Excel (and only in Excel), then maybe you should generate an actual Excel file (e.g. using Apache POI) instead of generating a CSV file.

Using I/O stream to parse CSV file

I have a CSV file of US population data for every county in the US. I need to get each population from the 8th column of the file. I'm using a fileReader() and bufferedStream() and not sure how to use the split method to accomplish this. I know this isn't much information but I know that I'll be using my args[0] as the destination in my class.
I'm at a loss to where to being to be honest.
import java.io.FileReader;
public class Main {
public static void main(String[] args) {
BufferedReader() buff = new BufferedReader(new FileReader(args[0]));
String
}
try {
}
}
The output should be an integer of the total US population. Any help with pointing me in the right direction would be great.
Don't reinvent the wheel, don't parse CSV yourself: use a library. Even such a simple format as CSV has nuances: fields can be escaped with quotes or unescaped, the file can have or have not a header and so on. Besides that you have to test and maintain the code you've wrote. So writing less code and reusing libraries is good.
There are a plenty of libraries for CSV in Java:
Apache Commons CSV
OpenCSV
Super CSV
Univocity
flatpack
IMHO, the first two are the most popular.
Here is an example for Apache Commons CSV:
final Reader in = new FileReader("counties.csv");
final Iterable<CSVRecord> records = CSVFormat.DEFAULT.parse(in);
for (final CSVRecord record : records) { // Simply iterate over the records via foreach loop. All the parsing is handler for you
String populationString = record.get(7); // Indexes are zero-based
String populationString = record.get("population"); // Or, if your file has headers, you can just use them
… // Do whatever you want with the population
}
Look how easy it is! And it will be similar with other parsers.

Writing a JSON string from Java to Excel in a formatted way

I am trying to write a JSON in string format to an excel file in Java. I know how to write to the file but I can't figure out how to format it.
For example if I wanted to write this:
{'data':[{'id':1,'name':'Eredivisie','active':true}]}
To an excel file how could I implement it so each value appears in its own separate cell? Like this: Screenshot of sample excel sheet
I have done research but can't find a simple answer. Any help would be appreciated, thanks.
The CSV (comma separated values) file format is a good choice.
So you would just output text to a file, separating each value with a comma. E.G:
ID,Name,Active
1,Eredivisie,true
...etc
Depending on your data (if it contains any commas), you may have to choose a different character to separate the values. Good job that in excel you can specify which character you are using.
Just parse the JSON as simple POJO object and then format the POJO object as string to create a CSV format.
The code will look some thing like this:
ObjectMapper mapper = new ObjectMapper();
List<Data> dataList = mapper.readValue(json, List.class);
StringBuilder csvFormattedText = new StringBuilder("id, name, active");
forEach (Data data : dataList) {
scvFormattedText.append(data.getId()).append(", ")
.append(data.getName)
.append(", ")
.append(data.getActiveStatus())
.append("\n");
}
File csvFile = new File("fromJSON.csv");
try ( PrintWriter write = new PrintWriter(csvFile)) {
out.println(csvFormattedText.toString());
}
Here Data is the POJO class which is created based on the fields in JSON.
In this way, you can convert the JSON to CSV file and thus it can be opened using excel.
If you dont like this approach, you may have to use libraries like Apache POI to convert JSON to xls. You have to follow same process, only difference it POI help you to set the values in each cell of the spreadsheet.
Further reading on POI library: Apache POI
Hope it helps.
you don't need to do something more just pull the json and then apply them to write in excel ,create new row then create new 4 cells in each object
JSONObject json=new JSONObject("{\"data\":[{\"id\":1,\"name\":\"Eredivisie\",\"active\":true}]}
JSONObject data=new json.getJSONObject("data"));
cell1.setCellStringValue(data.getString("id"));
cell2.setCellStringValue(data.getString("name"));
cell3.setCellStringValue(data.getString("Eredivisie"));
cell4.setCellStringValue(data.getString("active"));

CSV and Big Values

I have string value.
The value is :
12345.123456789012345
I want to write this value to csv file from java.I use OpenCSV for this.
Here is the code.
String csv = "D:\\denemehttp\\dene.csv";
CSVWriter writer = new CSVWriter(new FileWriter(csv));
String [] country = "India#China#United States#12345.1234567890123456".split("#");
writer.writeNext(country);
writer.close();
But when I open the csv the value is :
12345.123456789
But it must be:
12345.123456789012345
And note that when I open the csv file with Notepad++ it show the true value:
12345.123456789012345
So what is the problem?
Edit:I found the solution.I try Apache POI and it works.You can set the cell type with APACHE POI and excell doesn't see the value as a number and doesn't format it.It writes the value as a string completely.
You mention that you opened up the CSV in two different applications. The first one either clips it or formats it so you don't see the true value (eg. Excel). Your program is correct.
If the application you are using is Excel which you are using to open, you need to enclose the value in double quotes (") before you write it.
The csv should look like:
"India","China","United States","12345.1234567890123456"
This is because Excel does not recognize the number with this amount of precision, it needs to be treated as a string.

CSV Generation, Date and Long number formats

I am generating a CSV file on the fly/runtime with JSF and the code is as follows
FacesContext context = FacesContext.getCurrentInstance();
HttpServletResponse response = HttpServletResponse)context.getExternalContext().getResponse();
int read = 0;
byte[] bytes = new byte[1024];
response.setContentType("text/csv");
response.setHeader("Content-Disposition", "attachment;filename=\"" + fileName + "\"");
ServletOutputStream os = null;
StringBuffer stringBuffer1 = new StringBuffer("");
stringBuffer1.append("Disconnect Time");
stringBuffer1.append(',');
stringBuffer1.append("Calling Number");
stringBuffer1.append("01/06/2010 01:00:35 AM");
stringBuffer1.append(", ");
stringBuffer1.append("447744369900");
ByteArrayInputStream bis1;
try {
bis1 = new ByteArrayInputStream(stringBuffer1.toString().getBytes("UTF-8"));
os = response.getOutputStream();
while ((read = bis1.read(bytes)) != -1) {
os.write(bytes, 0, read);
}
os.flush();
os.close();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
FacesContext.getCurrentInstance().responseComplete();
and below is the content of the file when opened
Disconnect Time Calling Number
1/6/2010 1:00 4.47744E+11
The Actual expected result will be complete date format w.r.t AM/PM and number in complete length.
I have already tried example of double quotes as given Excel CSV - Number cell format and some other of adding blank space but they did not worked. Also the user will be performing arithmetic operation on the number columns.
Thanks in advance.
I'm no JSF expert but this seems entirely related to how Excel reads this information.
I tried playing a bit with the data, but could not make it do what you want it to do in CSV.
I created a sample CSV file with this content (using Nopepad++):
Disconnect Time, Calling Number
01/06/2010 01:00:35 AM,447744369900
And when I viewed it using Excel it gave me the same output you got - so the question you are facing is related to showing CSV in Excel - and not JSF.
You've mentioned that you're generating the file dynamically. If you are exporting data from a dataTable you might want to consider using the p:dataExporter component from PrimeFaces (you'll have to use a p:dataTable though). It may save you programatically creating the excel file if it's already in the correct structure in your dataTable.
Then it's as easy as attaching p:dataExporter to a link, and pointing it to your dataTable (tbl in this case).
<h:commandLink>
<p:graphicImage value="/images/excel.png" />
<p:dataExporter type="xls" target="tbl" fileName="cars" />
</h:commandLink>
It also supports CSV and PDF by the way.
RonK is right. There is no way you can tell Excel how to format the data when using a CSV file. Excel just uses a best guess as to what the data in the CSV columns are and then applies the default format for that type of data.
It is very likely that Excel still has the correct data (i.e. still has the seconds of your time column) and it is just an issue with formatting in Excel.
Have you tried applying a different format in Excel?
You will either have to tell the users how to format the data correctly in Excel or you have to create an Excel file directly (e.g. using Apache POI) and applying the correct format that way.
Sorry, looks like a bug ("feature", "deficit") in Excel.
It assumes anything that looks like a number is a number (and loses precision when reading it).
The simplest way probably is to ensure that the phone numbers do not look like numbers.
For example by including ( and similar characters.
Remeber that in Excel you are sometimes expected to write '=123+456 if you don't want it to compute the equation. Try this, too.
Date is probably just formatting, try formatting the column to include seconds.
To show correct format for dates and numbers in CSV I used =("447744369900") approach. The disadvantage is this that we cannot perform arithmetic operations on the cell. Now as Discussed with RonK and Turismo I am now shifted to Apache Excel library that allows you to define the format of the Excel file and create a XLS file instead of CSV. It is little slower than CSV but fulfills my requirements. See API to write huge excel files using java
Thanks again RonK and Turismo
After you open the CSV in excel, you can change the format of the "calling number" cells as Number. Select column -> Format Cells -> Change to Number

Categories