[Training Video - 6] [File Reading] [Java] Create and Write 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 :

[Training Video - 6] [File Reading] [Java] Create and Write Excel File Using Apache POI API的更多相关文章
- [Training Video - 6] [File Reading] [Java] Read Excel File Using Apache POI API
读取以下两种格式的Excel : *.xls and *.xlsx 用Apache POI API来实现,需要用到 HSSF 和 XSSF 的类库 HSSF is the POI Project's ...
- [Training Video - 6] [File Reading] [Java] Read Properties file
package com.file.properties; import java.io.FileInputStream; import java.util.Properties; public cla ...
- java word转html 报错 org/apache/poi/xwpf/usermodel/IRunBody
最终解决的办法是修改jar包版本,一定要对应上. <dependency> <groupId>org.apache.poi</groupId> <artifa ...
- Apache POI – Reading and Writing Excel file in Java
来源于:https://www.mkyong.com/java/apache-poi-reading-and-writing-excel-file-in-java/ In this article, ...
- How to Read, Write XLSX File in Java - Apach POI Example---reference
No matter how Microsoft is doing in comparison with Google, Microsoft Office is still the most used ...
- 10、借助POI实现Java生成并打印excel报表(1)
10.1.了解 Apache POI 实际开发中,用到最多的是把数据库中数据导出生成报表,尤其是在生产管理或者财务系统中用的非常普遍.生成报表格式一般是EXCEL或者PDF .利用Apache PO ...
- java用org.apache.poi包操作excel
一.POI简介 Jakarta POI 是apache的子项目,目标是处理ole2对象.它提供了一组操纵Windows文档的Java API 目前比较成熟的是HSSF接口,处理MS Excel(97- ...
- Java Annotation 应用 -- 导出Excel表格
相关知识链接: Introspector(内省) POI 1.声明注解 package com.ciic.component.excel; import java.lang.annotation.El ...
- Analysis about different methods for reading and writing file in Java language
referee:Java Programming Tutorial Advanced Input & Output (I/O) JDK 1.4+ introduced the so-calle ...
随机推荐
- RandomStringUtils工具类(java随机生成字符串)
使用RandomStringUtils可以选择生成随机字符串,可以是全字母,全数字,自定义生成字符等等... 其最基础的方法: 参数解读: count:需要生成的随机串位数 letters:只要字母 ...
- 【转】使用JMeter做性能测试的心得
企业应用开发过程中,性能测试是很重要的一个环节,在这个环节中Apache的JMeter以它开源.100%纯Java.操作方便等优点发挥着很大的作用. 经过一段时间的使用,多少有些心得和技巧,拿出来共享 ...
- Vue.js: temple
ylbtech-Vue.js: temple 1.返回顶部 2.返回顶部 3.返回顶部 4.返回顶部 5.返回顶部 6.返回顶部 7.返回顶部 8.返回顶部 9.返 ...
- Nginx限制服务地址
今天要在Nginx上设置禁止通过IP访问服务器,只能通过域名访问,这样做是为了避免别人把未备案的域名解析到自己的服务器IP而导致服务器被断网,从网络上搜到以下解决方案. 我们在使用的时候会遇到很多的恶 ...
- activemq的启动方式
一.简介:ActiveMQ 是Apache出品,最流行的,能力强劲的开源消息总线.ActiveMQ 是一个完全支持JMS1.1和J2EE 1.4规范的 JMS Provider实现,尽管JMS规范出台 ...
- Linux下压缩与解压
转自:http://www.mike.org.cn/blog/index.php?load=read&id=218###pp=0 [在解压或压缩的时候,一般还使用-v选项来现实正在处理的文件信 ...
- 爬虫高性能相关(协程效率最高,IO密集型)
一背景常识 爬虫的本质就是一个socket客户端与服务端的通信过程,如果我们有多个url待爬取,采用串行的方式执行,只能等待爬取一个结束后才能继续下一个,效率会非常低. 需要强调的是:串行并不意味着低 ...
- setTimeout和setInterval的unref()和ref()用法
var testFunction=function(){ console.log("guoyansi"); } var timer=setInterval(testFunction ...
- Windows远程桌面连接CentOS 7
1. 安装tigervnc-server yum install tigervnc-server 2. 设置vncserver服务器 将默认提供的文件复制到/etc/systemd/system,命令 ...
- 记一次爬需要登录之后才能爬取数据的demo
一:工程概况 注意: 二:涉及到的类 package com.bigdata.crawler; import java.io.IOException; import java.util.List; i ...