Showing posts with label apache. Show all posts
Showing posts with label apache. Show all posts

Wednesday, August 27, 2014

Write Excel File Using Apache POI in Java

Hi All,

Editing Microsoft documents using a java program might be useful in certain cases. For that there are various libraries. Here I'm going to show you how you can use the Apache POI with Microsoft Documents. More details are available here.

I had to use Apache POI to create and write values received from Arduino, to an Excel Sheet. So here I'm going to show you how you can write values to an Excel Sheet.

First download the latest release from Apache POI. It can be downloaded from here.

Create a new project in Eclipse and create a new class named 'WriteExcel.java'. Add a new folder to the project named 'lib'. Extract the Apache POI library and copy the 'poi-3.10.1-20140818.jar' to the lib folder. Then add the jar to the project by Right Click the Project -> Build Path -> Configure Build Path -> Add JARs.



Now we can start coding the 'WriteExcel.java' class.
Following is the complete code.

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

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.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.util.WorkbookUtil;


public class WriteExcel {
 
 public static void main(String[] args) {
  Workbook workbook = new HSSFWorkbook(); //Create a workbook object
  
  //Create a sheet inside the workbook. There are several ways to do this. 
  //1.Create sheet with default name
  Sheet sheet1 = workbook.createSheet(); 
  //2.Create Sheet with Valid Name
  Sheet sheet2 = workbook.createSheet("ValidName"); 
  //3.Create Sheet with any (may be invalid) name
  //createSafeSheetName() will make it correct
  Sheet sheet3 = workbook.createSheet(WorkbookUtil.createSafeSheetName("%?%#23InvalidName"));
  
  //Let's write some values to rows and columns in sheet1
  
  for (int i = 0; i < 20; i++) {
   //Create a row in sheet 1 assign it to "Row" object.
   Row row = sheet1.createRow(i);
   for (int j = 0; j < 10; j++) {
    //Create a cell in 'row' and assign it to "Cell" object
    Cell cell = row.createCell(j);
    //Write Values to Cell
    cell.setCellValue("( "+i+","+j+" )");
   }
  }
  
  try {
   //Create a new file output stream to write data to a File
   FileOutputStream fileOutputStream = new FileOutputStream("FirstExcel.xls");
   //Write values to the file
   workbook.write(fileOutputStream);
   //Cose file output stream
   fileOutputStream.close();
  } catch (FileNotFoundException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
 }
 
}

Now run the file and once you refresh the project, you'll see the "FirstExcel.xls" file added to your project. Open it. Following is the output.


Hope that helps, 
Thank You. :-) 

Saturday, August 16, 2014

XAMPP : Error: Apache shutdown unexpectedly.

Hi All, 

This is just a small blog post for trouble shooting the issues in installing the XAMPP server in Windows 8.1. I downloaded the setup from XAMPP site and installed it. When I tried to start the Apache service I got the following error. 

8:56:56 PM  [Apache] Attempting to start Apache app...
8:56:56 PM  [Apache] Status change detected: running
8:56:57 PM  [Apache] Status change detected: stopped
8:56:57 PM  [Apache] Error: Apache shutdown unexpectedly.
8:56:57 PM  [Apache] This may be due to a blocked port, missing dependencies, 
8:56:57 PM  [Apache] improper privileges, a crash, or a shutdown by another method.
8:56:57 PM  [Apache] Press the Logs button to view error logs and check
8:56:57 PM  [Apache] the Windows Event Viewer for more clues
8:56:57 PM  [Apache] If you need more help, copy and post this
8:56:57 PM  [Apache] entire log window on the forums

One main reason for this issue is that port 80 and 443 are used by other applications. One application is Skype. To solve this, you can go to Skype Options-> Advanced Settings -> Connection -> Uncheck the Use port 80 and 443 for additional incoming connections. 



Another application is VMWare. If  the issue caused by this, open the task manager, go to services and stop the VMWareHostd service. 



Doing one of the above changes will most probably solve your problem. 

Hope that helps. 

Thank you :-)