[Java]Create Excel File Using Apache POI API

package com.file.properties;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

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.RichTextString;
import org.apache.poi.ss.usermodel.Row;

public class CreateNewExcel {
		static void createExcelFile(File createFile,String sheetName, Map<String, Object[]> data) {	

		HSSFWorkbook workbook = new HSSFWorkbook();
		HSSFSheet sheet = workbook.createSheet(sheetName);

		Set<String> keySet=data.keySet();
		List<String> keyList=new ArrayList<String>(keySet);
		Collections.sort(keyList);

		int rownum = 0;
		for (String key : keyList) {
			Row row = sheet.createRow(rownum++);
			Object[] objArr = (Object[]) data.get(key);
			int cellnum = 0;
			for (Object obj : objArr) {
				Cell cell = row.createCell(cellnum++);
				if(obj instanceof Date)
					cell.setCellValue((RichTextString)obj);
				else if(obj instanceof Boolean)
					cell.setCellValue((Boolean)obj);
				else if(obj instanceof String)
					cell.setCellValue((String)obj);
				else if(obj instanceof Double)
					cell.setCellValue((Double)obj);
			}
		}

		try {
			FileOutputStream out = new FileOutputStream(createFile);
			workbook.write(out);
			out.close();
			System.out.println("Excel written successfully..");

		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		File createFile = new File("D:\\SoapUIStudy\\createExcelFile.xls");
		String sheetName = "Sample sheet";
		Map<String, Object[]> data = new HashMap<String, Object[]>();
		data.put("1", new Object[] {"Emp No.", "Name", "Salary"});
		data.put("2", new Object[] {1d, "John", 1500000d});
		data.put("3", new Object[] {2d, "Sam", 800000d});
		data.put("4", new Object[] {3d, "Dean", 700000d});
		createExcelFile(createFile,sheetName,data);
	}

}

Result :

时间: 2024-11-11 09:29:26

[Java]Create Excel File Using Apache POI API的相关文章

[Java]Read Excel File Using Apache POI API

读取以下两种格式的Excel : *.xls  and *.xlsx 用Apache POI API来实现,需要用到 HSSF 和 XSSF 的类库 HSSF is the POI Project's pure Java implementation of the Excel '97(-2007) (.xls) file format. XSSF is the POI Project's pure Java implementation of the Excel 2007 OOXML (.xls

[Java]Write Excel File Using Apache POI API

package com.file.properties; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.InputStream; import java.util.Hashtable; import org.apache.poi.hssf.usermodel.HSSFCellStyle; import org.apache.poi.hssf.

Create Excel file in java using PoI

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 8

java操作excel(转)poi.jar

import java.io.BufferedInputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.text.DecimalFormat; import java.text.SimpleDateFormat; import java.util.ArrayList;

Java 操作 Excel (读取Excel2007,Poi实现)

关于Java读取Excel2007的文章在Google.百度上搜索一下,没有太好的例子,实现的也不算太好.查看了一下Poi,最新的 POI 3.5 beta 4 支持读写 Excel2007和PPT2007(XLSX and PPTX),自己来实现Java读取Excel2007了. 1,下载 POI 3.5 beta 4 解压,把其中的jar包导入项目文件.以我的读取为例,导入了以下jar包.  没有配置 log4j,测试时报告警报信息,应该为加载顺序导致的初始化问题造成(暂时没有找原因). 2

[转]How to insert a row between two rows in an existing excel with HSSF (Apache POI)

本文转自:http://stackoverflow.com/questions/5785724/how-to-insert-a-row-between-two-rows-in-an-existing-excel-with-hssf-apache-poi Somehow I manage to create new rows between two rows in an existing excel file. The problem is, some of the formatting were

POI 操作Excel 异常org.apache.poi.openxml4j.exceptions.invalidformatexception: package should contain a c

POI 操作Excel 出现如下异常 org.apache.poi.openxml4j.exceptions.invalidformatexception: package should contain a content type part 代码如下 public boolean parseToExcel(String oldFileName,String newFileName){ Map<Object,Object> map = new HashMap<Object,Object&

java 操作excel文档 使用poi

简答你的使用  方便以后使用到时查看 1 package kite.poi; 2 3 import java.awt.Font; 4 import java.io.File; 5 import java.io.FileOutputStream; 6 import java.util.Calendar; 7 import java.util.Date; 8 9 import org.apache.poi.hssf.usermodel.HSSFCell; 10 import org.apache.p

How to create Excel file in C#

http://csharp.net-informations.com/excel/csharp-create-excel.htm Before you create an Excel file in C# , you have to add the Microsoft Excel 12.0 Object Library to you project.