Pages Navigation Menu

Coding is much easier than you think

Parsing / Reading XLS -XLSX -CSV-XML file in Java

Advertisements

Converting XLS to CSV files Using Java

Posted by on Mar 16, 2013 in CSV, Java, Java Excel | 14 comments

Converting XLS to CSV files Using Java

  package com.simplecode.excel; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.util.Iterator; import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Row; class XlstoCSV { static void xls(File inputFile, File outputFile) { // For storing data into CSV files StringBuffer data =...

read more

How to delete file if exists in java?

Posted by on Mar 11, 2013 in Java, Java I/O | 0 comments

How to delete file if exists in java?

  package com.simplecode.com; import java.io.File; public class FileDemo { public static void main(String[] args) { try{ String tempFile = "C:/mydir/myfile.txt"; //Delete if tempFile exists File fileTemp = new File(tempFile); if (fileTemp.exists()){ fileTemp.delete(); } }catch(Exception e){ // if any error occurs e.printStackTrace(); } } }

read more

How To Delete File In Java

Posted by on Mar 11, 2013 in Java, Java I/O | 1 comment

  The command File.delete() to delete a file in java, it will return a boolean value to indicate the delete operation status; true if the file is deleted; false if failed.   Example   In this example, it will delete a log file named “C:\\sample.txt”   package com.simplecode.file; import java.io.File; public class DeleteFile { public static void main(String[] args) { try{ File file = new File("c:\sample.txt"); if(file.delete()){ ...

read more

How To Check If A File Exists In Java

Posted by on Mar 11, 2013 in Java, Java I/O | 0 comments

  To determine whether a file is exist in your file system, use the Java IO File.exists().   package com.simplecode.file; import java.io.File; public class DeleteFile { public static void main(String[] args) { try { File file = new File("c:\file.txt"); if(file.exists()){ System.out.println(file.getName() + " exists!"); }else{ System.out.println("File does not exists."); } } catch(Exception e) ...

read more

Java Code to Read XLS and XLSX Files

Posted by on Mar 10, 2013 in Java Excel | 18 comments

  Note : This Program uses XSSFWorkbook for xlsx and HSSFWorkbook for xlx.   package com.simplecode.excel; import java.io.*; import java.util.Iterator; import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.xssf.usermodel.XSSFSheet; import org.apache.poi.xssf.usermodel.XSSFWorkbook; class ExcelReader { static void readXlsx(File inputFile) { try { // Get...

read more

Read Excel with Java (Jexcel-api)

Posted by on Feb 17, 2013 in Java, Java Excel | 0 comments

Read Excel with Java  (Jexcel-api)

  In our previous article we have In our next article I have implemented Write Excel in Java using Jexcel-api in this article demonstrate how to read Excel files with the Java Excel API.   Since I have explained about Jexcel api in my previous article, so I’m not going to explain here again.   Download – Java Excel library   package com.simplecode.reader; import java.io.File; import java.io.IOException; import jxl.Cell; import jxl.CellType; import jxl.Sheet; import...

read more

Write Excel with Java (Jexcel-api)

Posted by on Feb 16, 2013 in Java, Java Excel | 2 comments

Write Excel with Java (Jexcel-api)

  This article demonstrate how to create Excel files with the Java Excel API.   ExcelApi – one of a number of open-source java Excel API, designed for reading, writing, and editing of dynamic excel. Among its features are: Reading from the book Excel 95, 97, 2000, XP, and 2003 Reading and writing formulas Creating a list in Excel 2000 Support for formatting fonts, numbers and dates Support colors, borders, and colors of cells Changing existing books excel Localization support Copying charts Copying and pasting drawings Logging...

read more

Read / Write CSV file in Java using opencsv library

Posted by on Oct 10, 2012 in CSV, Java | 7 comments

  We often need to read data from a CSV file to do some manipulation. Most of the examples I have seen uses StringTokenizer to read the CSV file but that has certain limitations. It cannot read a CSV file properly if the data present in the CSV has a comma in them. We can avoid this issue by using a open source project called Opencsv   You can use this library to Create, Read and write CSV files. The best part of OpenCSV parser is, it takes a CSV file and map the result data to a Java Bean object.   Please download and add...

read more

Java code to Auto size columns in Excel files created with Apache POI

Posted by on Sep 9, 2012 in Java, Java Excel | 4 comments

Java code to Auto size columns in Excel files created with Apache POI

  This was the reports created by my java application and it turned out to be quite irritating as the columns are not auto sized.     The solution to the above problem is you just need to call a method to auto fit the column width. Here is the code to auto size the first 5 columns of the spread sheet.   HSSFSheet sheet = workBook.createSheet("testXls"); // Code to Auto size the column widths for(int columnPosition = 0; columnPosition< 5; columnPosition++) { ...

read more
%d bloggers like this: