Convert CSV to excel File

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.









Related Tutorials/Questions & Answers:
Convert CSV to excel File - Java Beginners
Convert CSV to excel File  Sir, i have CSV file as input. I need to convert that CSV file to excel file and format. Currently i am using HSSF POI... the CSV file ann convert the same into excel file. If posibble pls provide me
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
Advertisements
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
how to convert .xml file to csv file
how to convert .xml file to csv file  how to convert .xml file to csv file
convert .txt file in .csv format - Java Beginners
convert .txt file in .csv format  Dear all, I hope you are doing good. I am looking to convert .txt file in .csv format. The contents might have... want export these contents to a .csv file. I am aware that I need to use
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
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
convert excel into pdf
convert excel into pdf  How to convert excel into pdf files
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 '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 '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
.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
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
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
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:ADS_TO_REPLACE_1 Example import java.io.*; import
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
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
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
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 '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
importing excel file
importing excel file  Hi All I am developing a java application whre we upload a excel file in to database and all excel value should import... it is not showing real excel sheet value. please suggest good way to solve this problem
problem while reading .csv file
problem while reading .csv file  I have a problem here..... i am reading a .csv extention file which has some value....but the problem is der is an amount column which contains , in between (eg) 3,899.00 which inturns creates
php csv file uploding into mysql database table.
php csv file uploding into mysql database table.  hai friends, i have two excel files with different field names.each files having more than 30... uploding csv file, i want to fetch the datas in corresponding fields in table. can
How do I import a CSV file in R?
How do I import a CSV file in R?  Hi, I have a CSV file with 900000 records and I want to load it for analytics. How do I import a CSV file in R... analytics on cluster. It provides support for getting data from database, excel
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
excel file using JDBC java.?
excel file using JDBC java.?  hey, i am a telecomm. engineer , and i am try to develop a tool which reads from a excel file and then appends the same excel file as required. i am writing my code on notepad and have successfully
importing excel file and drawing multiple graphs from one excel file
importing excel file and drawing multiple graphs from one excel file  thanks a lot sir for replying with code for importing excel file... time from one excel file using different columns..and instead of passing column
Excel file wriring using java
Excel file wriring using java  i have one predefined bill format in excel i want to append values in it using java apche POI or Jexcel i tried it but it overwrites file and lost previous contents what can i do
Reading and querying an Excel file in JavaScript
Reading and querying an Excel file in JavaScript  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
write excel file into the oracle database
write excel file into the oracle database  dear sir, i need the jsp code that reads the excel file and stores it into the oracle database table..and also i need the code to connect oracle database? thank u in advance
how to use Excel Template to write excel file using java
how to use Excel Template to write excel file using java  How to use Excel template to write data in that file using java
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
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 a file .doc to .zip
convert a file .doc to .zip  hiii, I uploaded a file...;TITLE>Display file upload form to the user</TITLE></HEAD> <...;td><b>Choose the file To Upload:</b></td> <td><
reading a csv file from a particular row
reading a csv file from a particular row  how to read a csv file from a particular row and storing data to sql server by using jsp servlet
Import Excel file(date) into mysql database in php
Import Excel file(date) into mysql database in php  I try to import excel file which contains data date(yyyy-mm-dd) into mysql database in php...']['tmp_name']; $handle = fopen('data.csv',"r"); //loop through the csv file
Linking an Excel File with the Application tool
Linking an Excel File with the Application tool  I have to link an excel file with a application software which I am developing.I have this code... on the questions that are in the excel file like extracting the questions from the excel file
how to create an excel file using java
how to create an excel file using java  how to create an excel file using java
how to insert excel file into mysql datbase in servlet
how to insert excel file into mysql datbase in servlet  emphashow to insert excel file into mysql datbase inservletized text
how to parse a csv file using standard libraries?
how to parse a csv file using standard libraries?  hie i am a beginner in java i want to parse a csv file using any standard libraries i want to know how the libraries are imported and used in eclipse thanks in advance
create csv file in php in multiple columns
create csv file in php in multiple columns  i need create csv file in multiple columns like this: id name age date_added 1 john 23 2012-2-12 2 jane 25 2012-3-14 i can build csv file like this: id,name,age,date
create csv file in php in multiple columns
create csv file in php in multiple columns  i need create csv file in multiple columns like this: id name age date_added 1 john 23 2012-2-12 2 jane 25 2012-3-14 i can build csv file like this: id,name,age,date
java, upload csv file - Development process
java, upload csv file  hi, i have a csv file having 29 colums, i want to upload that csv file to database. i need the code,, can u please send me the code.. thanks..  Hi Friend, Please visit the following
connection of java file to excel file and put data from excel file into oracle db
connection of java file to excel file and put data from excel file into oracle db  how to create button on excel sheet using java swing and how we give action to that button excel sheet? how we connect excel file to java file

Ads