Solved

Writing to an Excel File using Apache POI

Posted on 2012-03-15
3
844 Views
Last Modified: 2012-06-27
Hi, I have a HashMap with some 3000 key value pairs and I want to print all the values of the HashMap to a Excel sheet. Can someone give me the complete code if you dont mind. Thank you.
0
Comment
Question by:vgsrikanth
  • 2
3 Comments
 
LVL 47

Expert Comment

by:for_yan
ID: 37727468
This is your complete code, which puts three pairs form Hashmpa onto Excel Spreadsheet
You'll see

two b
one a
three c

in the to three rows of your excel file

Tested it with poi-3.7.jar

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

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFDataFormat;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor;

public class HSSFCreate {

	public static void main(String[] args) {
		try {

            HashMap<String, String> map = new HashMap<String, String>();
            map.put("one","a");
            map.put("two","b");
             map.put("three","c");

         //   Enumeration en = map.keys();

            Set<Map.Entry<String,String>> ss = map.entrySet();



			FileOutputStream fileOut = new FileOutputStream("poi-test.xls");
			HSSFWorkbook workbook = new HSSFWorkbook();
			HSSFSheet worksheet = workbook.createSheet("POI Worksheet");

			// index from 0,0... cell A1 is cell(0,0)

            int j = 0;
            for(Map.Entry<String,String> men : ss){

			HSSFRow row1 = worksheet.createRow((short) j);
                j++;

			HSSFCell cellA1 = row1.createCell((short) 0);
			cellA1.setCellValue(men.getKey());
			HSSFCellStyle cellStyle = workbook.createCellStyle();
			cellStyle.setFillForegroundColor(HSSFColor.GOLD.index);
			cellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
			cellA1.setCellStyle(cellStyle);

			HSSFCell cellB1 = row1.createCell((short) 1);
			cellB1.setCellValue(men.getValue());
			cellStyle = workbook.createCellStyle();
			cellStyle.setFillForegroundColor(HSSFColor.LIGHT_CORNFLOWER_BLUE.index);
			cellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
			cellB1.setCellStyle(cellStyle);

			HSSFCell cellC1 = row1.createCell((short) 2);
			cellC1.setCellValue(true);

			HSSFCell cellD1 = row1.createCell((short) 3);
			cellD1.setCellValue(new Date());
			cellStyle = workbook.createCellStyle();
			cellStyle.setDataFormat(HSSFDataFormat
					.getBuiltinFormat("m/d/yy h:mm"));
			cellD1.setCellStyle(cellStyle);
            }

			workbook.write(fileOut);
			fileOut.flush();
			fileOut.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

}

Open in new window

0
 

Author Comment

by:vgsrikanth
ID: 37727533
Thank you Yan for the solution.My concern is if I have to print 3000 values then do i have to create 3000 cells using createcell() function. If you dont mind can you give code to print 3000 values of HashMap.
0
 
LVL 47

Accepted Solution

by:
for_yan earned 500 total points
ID: 37727594
Don't worry, just remove the style - it is too much with style
with this code i wrote 3000 rows - two cell in each row - and without
any problems
 
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.*;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFDataFormat;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor;

public class HSSFCreate {

	public static void main(String[] args) {
		try {

            HashMap<String, String> map = new HashMap<String, String>();
            map.put("one","a");
            map.put("two","b");
             map.put("three","c");

         //   Enumeration en = map.keys();

            Set<Map.Entry<String,String>> ss = map.entrySet();



			FileOutputStream fileOut = new FileOutputStream("poi-test.xls");
			HSSFWorkbook workbook = new HSSFWorkbook();
			HSSFSheet worksheet = workbook.createSheet("POI Worksheet");

			// index from 0,0... cell A1 is cell(0,0)

            int j = 0;
     

                    for(int jj=0; jj<3000; jj++){

			HSSFRow row1 = worksheet.createRow((short) jj);
                j++;

			HSSFCell cellA1 = row1.createCell((short) 0);
			cellA1.setCellValue("Heelo");


			HSSFCell cellB1 = row1.createCell((short) 1);
			cellB1.setCellValue("OK");
	

			HSSFCell cellC1 = row1.createCell((short) 2);
			cellC1.setCellValue(true);

			HSSFCell cellD1 = row1.createCell((short) 3);

            }

			workbook.write(fileOut);
			fileOut.flush();
			fileOut.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

}

Open in new window

0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

785 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question