Convert CSV to excel File

Ads
 

Convert CSV to excel File

View Answers

September 16, 2008 at 5:53 PM

Hi friend,



Code to convert csv to xls file.

Some points to be remember.


1."test.csv" is the name of the csv file .

2.create a folder "excel" in c: drive.

3."xfile.xls" name of the .xls file create in the "excel" folder.


import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.StringTokenizer;
import java.util.*;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.hssf.util.*;
import java.io.*;

public class CsvToXls
{
public static void main(String args[]) throws IOException
{
ArrayList arList=null;
ArrayList al=null;
String fName = "test.csv";
String thisLine;
int count=0;
FileInputStream fis = new FileInputStream(fName);
DataInputStream myInput = new DataInputStream(fis);
int i=0;
arList = new ArrayList();
while ((thisLine = myInput.readLine()) != null)
{
al = new ArrayList();
String strar[] = thisLine.split(",");
for(int j=0;j<strar.length;j++)
{
al.add(strar[j]);
}
arList.add(al);
System.out.println();
i++;
}

try
{
HSSFWorkbook hwb = new HSSFWorkbook();
HSSFSheet sheet = hwb.createSheet("new sheet");
for(int k=0;k<arList.size();k++)
{
ArrayList ardata = (ArrayList)arList.get(k);
System.out.println("ardata " + ardata.size());
HSSFRow row = sheet.createRow((short) 0+k);
for(int p=0;p<ardata.size();p++)
{
System.out.print(ardata.get(p));
HSSFCell cell = row.createCell((short) p);
cell.setCellValue(ardata.get(p).toString());
}
System.out.println();
}
FileOutputStream fileOut = new FileOutputStream("C:\\excel\\xfile.xls");
hwb.write(fileOut);
fileOut.close();
System.out.println("Your excel file has been generated");
} catch ( Exception ex ) {} //main method ends
}
}


Thanks

December 5, 2011 at 12:52 PM

Thank you so much for this excellent code


February 3, 2012 at 4:33 AM

Hi,

The code works awesome with 1 issue.I have a column that contains the value as statuscompleted,statussuccess in csv.But the program is considering statuscompleted and statussuccess as separate columns and the excel is a mess.is that something that can be rectified.Thanks for your help again.

Ads









Related Tutorials/Questions & Answers:
convert excel file data into different locales while converting exclile file to csv file
convert excel file data into different locales while converting exclile file to csv file   can any one provide the code for how to convert excel file data into different locales while converting exclile file to csv file
sir, how to convert excel file to csv file using java? please send me sample code.
sir, how to convert excel file to csv file using java? please send me sample code.  please send me sample code for converting excel file into csv file uisng java
Advertisements
how to convert .xml file to csv file
how to convert .xml file to csv file  how to convert .xml file to csv file
How to convert excel file int xml format in java
How to convert excel file int xml format in java  How to read excel file(xls) which is stored in any directory(D://) and convert it into xml format in java and place in any directory(E://).Can any body provide the code
upload a file into database and progrm should support excel and text and csv file formats
upload a file into database and progrm should support excel and text and csv file formats  Hai all, I need a program to upload a file into database table... and the program should support .excel ,.txt ,.csv file formats. can
convert excel into pdf
convert excel into pdf  How to convert excel into pdf files
ModuleNotFoundError: No module named 'csv-convert'
ModuleNotFoundError: No module named 'csv-convert'  Hi, My Python... 'csv-convert' How to remove the ModuleNotFoundError: No module named 'csv-convert' error? Thanks   Hi, In your python environment
convert html to excel using jsp
convert html to excel using jsp   i want to convert a html page into mcrosoft excel page using jsp.how i do
ModuleNotFoundError: No module named 'excel-csv-helpers'
ModuleNotFoundError: No module named 'excel-csv-helpers'  Hi, My... named 'excel-csv-helpers' How to remove the ModuleNotFoundError: No module named 'excel-csv-helpers' error? Thanks   Hi, In your
ModuleNotFoundError: No module named 'onemg-excel-csv'
ModuleNotFoundError: No module named 'onemg-excel-csv'  Hi, My... named 'onemg-excel-csv' How to remove the ModuleNotFoundError: No module named 'onemg-excel-csv' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'convert-csv-to-json'
ModuleNotFoundError: No module named 'convert-csv-to-json'  Hi, My... named 'convert-csv-to-json' How to remove the ModuleNotFoundError: No module named 'convert-csv-to-json' error? Thanks   Hi
ModuleNotFoundError: No module named 'convert-csv-to-xlsx'
ModuleNotFoundError: No module named 'convert-csv-to-xlsx'  Hi, My... named 'convert-csv-to-xlsx' How to remove the ModuleNotFoundError: No module named 'convert-csv-to-xlsx' error? Thanks   Hi
ModuleNotFoundError: No module named 'convert-csv-to-json'
ModuleNotFoundError: No module named 'convert-csv-to-json'  Hi, My... named 'convert-csv-to-json' How to remove the ModuleNotFoundError: No module named 'convert-csv-to-json' error? Thanks   Hi
ModuleNotFoundError: No module named 'convert-csv-to-xlsx'
ModuleNotFoundError: No module named 'convert-csv-to-xlsx'  Hi, My... named 'convert-csv-to-xlsx' How to remove the ModuleNotFoundError: No module named 'convert-csv-to-xlsx' error? Thanks   Hi
ModuleNotFoundError: No module named 'excel-convert-xml'
ModuleNotFoundError: No module named 'excel-convert-xml'  Hi, My... named 'excel-convert-xml' How to remove the ModuleNotFoundError: No module named 'excel-convert-xml' error? Thanks   Hi, In your
ModuleNotFoundError: No module named 'soc-excel-convert'
ModuleNotFoundError: No module named 'soc-excel-convert'  Hi, My... named 'soc-excel-convert' How to remove the ModuleNotFoundError: No module named 'soc-excel-convert' error? Thanks   Hi, In your
ModuleNotFoundError: No module named 'soc-excel-convert'
ModuleNotFoundError: No module named 'soc-excel-convert'  Hi, My... named 'soc-excel-convert' How to remove the ModuleNotFoundError: No module named 'soc-excel-convert' error? Thanks   Hi, In your
Reading an excel file into array
Reading an excel file into array  Hi, I'm trying to read in an excel file, search a column for containing key words (entered by a user) and then displaying the matching rows in a table. I'm fairly new to JavaScript. Can anyone
.csv file to databse
.csv file to databse  I have an 1 .csv file.. its having the url values like http://maps.google.com/?q=425+Bingeman+Centre+Drive%2c+chrompet%2c+ON%2c+600+006... q is request parameter .425 Bingeman Centre Drive is address
How to write into CSV file in Java
How to write into CSV file in Java  How to write into CSV file in Java   Hi, To create Comma separated value(CSV) file is very simple and the CSV file stores the value in the tabular form i.e. rows and columns
Java Read CSV file
Java Read CSV file In this tutorial, you will learn how to read csv file. CSV... to break the line using ",". The delimiter for a CSV file is a comma.... Here is a csv file: Example import java.io.*; import java.util.*; public
csv file download
csv file download  Hello Every one, when user clicks download button I want to retrieve data from mysql database table in csv format using java.if you know please send code. Thanks in Advance   Please visit
Java code to convert pdf file to word file
Java code to convert pdf file to word file  How to convert pdf file to word file using Java
Java Write To File CSV
Java Write To File CSV In this tutorial you will learn how to write to file... that is in a tabular form. In java to create a csv file is simple, you would require... a simple example which will demonstrate you how to write to csv file. To achieve
Convert Text File to PDF file
Convert Text File to PDF file  Here is the way how to covert your Text file to PDF File, public class TextFileToPDF { private static void... BufferedReader(new FileReader( "**INPUT FILE**")); String inLine
How to convert XML file to database ?
How to convert XML file to database ?  How to convert XML file to database
Excel file Handling in Java
Excel file Handling in Java  Hello Sir, I am new to Java, I have Started Java Core by myself. I want to make a project which include 3-4 Excel file to handle.Please explain how to read and write Excel file in Java.Is it tough
how to convert a jar file into .exe file
how to convert a jar file into .exe file  hi, I want convert my jar file into executable file,urgently please help me
how to use Excel Templet to write excel file using java.
how to use Excel Templet to write excel file using java.  how to use Excel Templet to write excel file using java
Convert Text File to PDF file
Convert Text File to PDF file  import java.io.BufferedReader; import... FileReader( Input File)); String inLine = null... FileOutputStream( output file)); document.open(); document.add
convert zip file to byte array
convert zip file to byte array  Hi,How to convert zip file to byte array,can you please provide program?? Thanks, Ramanuja

Ads