SlideShare a Scribd company logo
Apache POI Tutorial
Apache POI is a popular API that allows programmers to create, modify, and
display MS Office files using Java programs. It is an open source library
developed and distributed by Apache Software Foundation to design or
modify Microsoft Office files using Java program. The name POI was
originally an acronym for Poor Obfuscation Implementation, referring
humorously to the fact that the file formats seemed to be deliberately
obfuscated, but poorly, since they were successfully reverse-engineered.
• HSSF (Horrible SpreadSheet Format) – reads and writes Microsoft Excel
(XLS) format files.
• XSSF (XML SpreadSheet Format) – reads and writes Office Open XML
(XLSX) format files.
• HPSF (Horrible Property Set Format) – reads “Document Summary”
information from Microsoft Office files.
• HWPF (Horrible Word Processor Format) – aims to read and write
Microsoft Word 97 (DOC) format files.
• HSLF (Horrible Slide Layout Format) – a pure Java implementation for
Microsoft PowerPoint files.
• HDGF (Horrible DiaGram Format) – an initial pure Java implementation
for Microsoft Visio binary files.
• HPBF (Horrible PuBlisher Format) – a pure Java implementation for
Microsoft Publisher files.
• HSMF (Horrible Stupid Mail Format) – a pure Java implementation for
Microsoft Outlook MSG files
• DDF (Dreadful Drawing Format) – a package for decoding the Microsoft
Office Drawing format.
• Working with .xlsx files
• The classes we used in above code
snippet, HSSFWorkbook and HSSFSheet works for .xls format. In order to
work with newer xls format viz .xlsx, you need to see newer POI classes
like:
• import org.apache.poi.hssf.usermodel.HSSFSheet;
• import org.apache.poi.xssf.usermodel.XSSFSheet;
• FileInputStream file = new FileInputStream(new File("C:test.xlsx"));
• //Get the workbook instance for XLS file
• XSSFWorkbook workbook = new XSSFWorkbook (file);
• //Get first sheet from the workbook XSSFSheet sheet =
workbook.getSheetAt(0);
• //Get iterator to all the rows in current sheet
• Iterator<Row> rowIterator = sheet.iterator();
• //Get iterator to all cells of current row
• Iterator<Cell> cellIterator = row.cellIterator();
Create Blank Workbook
• The following simple program is used to create a blank Microsoft
Excel Workbook.
• import java.io.*; import org.apache.poi.xssf.usermodel.*;
• public class CreateWorkBook {
• public static void main(String[] args)throws Exception {
• //Create Blank workbook
• XSSFWorkbook workbook = new XSSFWorkbook(); //Create file
system using specific name
• FileOutputStream out = new FileOutputStream( new
File("createworkbook.xlsx"));
• //write operation workbook using file out object
workbook.write(out); out.close(); System.out.println("
createworkbook.xlsx written successfully");
• } }
Open Existing Workbook
• Use the following code to open an existing workbook.
• import java.io.*; import org.apache.poi.xssf.usermodel.*;
• public class OpenWorkBook {
• public static void main(String args[])throws Exception { File file =
new File("openworkbook.xlsx"); FileInputStream fIP = new
FileInputStream(file);
• //Get the workbook instance for XLSX file
• XSSFWorkbook workbook = new XSSFWorkbook(fIP); if(file.isFile()
&& file.exists())
• { System.out.println( "openworkbook.xlsx file open successfully.");
• } else { System.out.println( "Error to open openworkbook.xlsx file.");
}
}
}
We will read above xls file using
Apache POI and prints the data.
• Consider a sample excel file:
• test.xls
• try {
•
• FileInputStream file = new FileInputStream(new
File("C:test.xls"));
•
• //Get the workbook instance for XLS file
• HSSFWorkbook workbook = new HSSFWorkbook(file);
• //Get first sheet from the workbook
• HSSFSheet sheet = workbook.getSheetAt(0);
•
• //Iterate through each rows from first sheet
• Iterator<Row> rowIterator = sheet.iterator();
• while(rowIterator.hasNext()) {
• Row row = rowIterator.next();
•
• //For each row, iterate through each columns
• Iterator<Cell> cellIterator = row.cellIterator();
• while(cellIterator.hasNext()) {
•
• Cell cell = cellIterator.next();
• switch(cell.getCellType()) {
• case
Cell.CELL_TYPE_BOOLEAN:
•
System.out.print(cell.getBooleanCellValue() + "tt");
• break;
• case
Cell.CELL_TYPE_NUMERIC:
•
System.out.print(cell.getNumericCellValue() + "tt");
• break;
• case
Cell.CELL_TYPE_STRING:
•
System.out.print(cell.getStringCellValue() + "tt");
• break;
• }
• }
• System.out.println("");
• }
• file.close();
• FileOutputStream out =
• new FileOutputStream(new
File("C:test.xls"));
• workbook.write(out);
• out.close();
•
• } catch (FileNotFoundException e) {
• e.printStackTrace();
• } catch (IOException e) {
• e.printStackTrace();
• }
• 3. Create New Excel File
• Let us create a new excel file and write data in it. Following is the API
which we will use for this purpose.
• import org.apache.poi.hssf.usermodel.HSSFSheet;
• import org.apache.poi.hssf.usermodel.HSSFWorkbook;
• //..
• HSSFWorkbook workbook = new HSSFWorkbook();
• HSSFSheet sheet = workbook.createSheet("Sample sheet");
• //Create a new row in current sheet
• Row row = sheet.createRow(0);
• //Create a new cell in current row
• Cell cell = row.createCell(0);
• //Set value to new value
• cell.setCellValue("Blahblah");
• Below is the complete code that writes a new excel with dummy data:
• HSSFWorkbook workbook = new HSSFWorkbook();
• HSSFSheet sheet = workbook.createSheet("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});
•
• Set<String> keyset = data.keySet();
• int rownum = 0;
• for (String key : keyset) {
• Row row = sheet.createRow(rownum++);
• Object [] objArr = data.get(key);
• int cellnum = 0;
• for (Object obj : objArr) {
• Cell cell = row.createCell(cellnum++);
• if(obj instanceof Date)
• cell.setCellValue((Date)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(new
File("C:new.xls"));
• workbook.write(out);
• out.close();
• System.out.println("Excel written
successfully..");
•
• } catch (FileNotFoundException e) {
• e.printStackTrace();
• } catch (IOException e) {
• e.printStackTrace();
• }
Output: new.xls
Ad

More Related Content

What's hot (20)

Apache Solr + ajax solr
Apache Solr + ajax solrApache Solr + ajax solr
Apache Solr + ajax solr
Net7
 
Introduction to Solr
Introduction to SolrIntroduction to Solr
Introduction to Solr
Erik Hatcher
 
Solr introduction
Solr introductionSolr introduction
Solr introduction
Lap Tran
 
Solr Masterclass Bangkok, June 2014
Solr Masterclass Bangkok, June 2014Solr Masterclass Bangkok, June 2014
Solr Masterclass Bangkok, June 2014
Alexandre Rafalovitch
 
Building your own search engine with Apache Solr
Building your own search engine with Apache SolrBuilding your own search engine with Apache Solr
Building your own search engine with Apache Solr
Biogeeks
 
Introduction to Lucene and Solr - 1
Introduction to Lucene and Solr - 1Introduction to Lucene and Solr - 1
Introduction to Lucene and Solr - 1
YI-CHING WU
 
Building a Search Engine Using Lucene
Building a Search Engine Using LuceneBuilding a Search Engine Using Lucene
Building a Search Engine Using Lucene
Abdelrahman Othman Helal
 
code4lib 2011 preconference: What's New in Solr (since 1.4.1)
code4lib 2011 preconference: What's New in Solr (since 1.4.1)code4lib 2011 preconference: What's New in Solr (since 1.4.1)
code4lib 2011 preconference: What's New in Solr (since 1.4.1)
Erik Hatcher
 
Beyond full-text searches with Lucene and Solr
Beyond full-text searches with Lucene and SolrBeyond full-text searches with Lucene and Solr
Beyond full-text searches with Lucene and Solr
Bertrand Delacretaz
 
Enterprise Search Solution: Apache SOLR. What's available and why it's so cool
Enterprise Search Solution: Apache SOLR. What's available and why it's so coolEnterprise Search Solution: Apache SOLR. What's available and why it's so cool
Enterprise Search Solution: Apache SOLR. What's available and why it's so cool
Ecommerce Solution Provider SysIQ
 
Lucene's Latest (for Libraries)
Lucene's Latest (for Libraries)Lucene's Latest (for Libraries)
Lucene's Latest (for Libraries)
Erik Hatcher
 
Lifecycle of a Solr Search Request - Chris "Hoss" Hostetter, Lucidworks
Lifecycle of a Solr Search Request - Chris "Hoss" Hostetter, LucidworksLifecycle of a Solr Search Request - Chris "Hoss" Hostetter, Lucidworks
Lifecycle of a Solr Search Request - Chris "Hoss" Hostetter, Lucidworks
Lucidworks
 
Rapid Prototyping with Solr
Rapid Prototyping with SolrRapid Prototyping with Solr
Rapid Prototyping with Solr
Erik Hatcher
 
Solr Black Belt Pre-conference
Solr Black Belt Pre-conferenceSolr Black Belt Pre-conference
Solr Black Belt Pre-conference
Erik Hatcher
 
Solr Recipes Workshop
Solr Recipes WorkshopSolr Recipes Workshop
Solr Recipes Workshop
Erik Hatcher
 
Integrating the Solr search engine
Integrating the Solr search engineIntegrating the Solr search engine
Integrating the Solr search engine
th0masr
 
Rapid Prototyping with Solr
Rapid Prototyping with SolrRapid Prototyping with Solr
Rapid Prototyping with Solr
Erik Hatcher
 
Apache Solr
Apache SolrApache Solr
Apache Solr
Minh Tran
 
Annotations
AnnotationsAnnotations
Annotations
Ostap Maliuvanchuk
 
Solr Indexing and Analysis Tricks
Solr Indexing and Analysis TricksSolr Indexing and Analysis Tricks
Solr Indexing and Analysis Tricks
Erik Hatcher
 
Apache Solr + ajax solr
Apache Solr + ajax solrApache Solr + ajax solr
Apache Solr + ajax solr
Net7
 
Introduction to Solr
Introduction to SolrIntroduction to Solr
Introduction to Solr
Erik Hatcher
 
Solr introduction
Solr introductionSolr introduction
Solr introduction
Lap Tran
 
Building your own search engine with Apache Solr
Building your own search engine with Apache SolrBuilding your own search engine with Apache Solr
Building your own search engine with Apache Solr
Biogeeks
 
Introduction to Lucene and Solr - 1
Introduction to Lucene and Solr - 1Introduction to Lucene and Solr - 1
Introduction to Lucene and Solr - 1
YI-CHING WU
 
code4lib 2011 preconference: What's New in Solr (since 1.4.1)
code4lib 2011 preconference: What's New in Solr (since 1.4.1)code4lib 2011 preconference: What's New in Solr (since 1.4.1)
code4lib 2011 preconference: What's New in Solr (since 1.4.1)
Erik Hatcher
 
Beyond full-text searches with Lucene and Solr
Beyond full-text searches with Lucene and SolrBeyond full-text searches with Lucene and Solr
Beyond full-text searches with Lucene and Solr
Bertrand Delacretaz
 
Enterprise Search Solution: Apache SOLR. What's available and why it's so cool
Enterprise Search Solution: Apache SOLR. What's available and why it's so coolEnterprise Search Solution: Apache SOLR. What's available and why it's so cool
Enterprise Search Solution: Apache SOLR. What's available and why it's so cool
Ecommerce Solution Provider SysIQ
 
Lucene's Latest (for Libraries)
Lucene's Latest (for Libraries)Lucene's Latest (for Libraries)
Lucene's Latest (for Libraries)
Erik Hatcher
 
Lifecycle of a Solr Search Request - Chris "Hoss" Hostetter, Lucidworks
Lifecycle of a Solr Search Request - Chris "Hoss" Hostetter, LucidworksLifecycle of a Solr Search Request - Chris "Hoss" Hostetter, Lucidworks
Lifecycle of a Solr Search Request - Chris "Hoss" Hostetter, Lucidworks
Lucidworks
 
Rapid Prototyping with Solr
Rapid Prototyping with SolrRapid Prototyping with Solr
Rapid Prototyping with Solr
Erik Hatcher
 
Solr Black Belt Pre-conference
Solr Black Belt Pre-conferenceSolr Black Belt Pre-conference
Solr Black Belt Pre-conference
Erik Hatcher
 
Solr Recipes Workshop
Solr Recipes WorkshopSolr Recipes Workshop
Solr Recipes Workshop
Erik Hatcher
 
Integrating the Solr search engine
Integrating the Solr search engineIntegrating the Solr search engine
Integrating the Solr search engine
th0masr
 
Rapid Prototyping with Solr
Rapid Prototyping with SolrRapid Prototyping with Solr
Rapid Prototyping with Solr
Erik Hatcher
 
Solr Indexing and Analysis Tricks
Solr Indexing and Analysis TricksSolr Indexing and Analysis Tricks
Solr Indexing and Analysis Tricks
Erik Hatcher
 

Similar to Apachepoitutorial (20)

How to Read Excel Files in Java (1).pdf
How to Read Excel Files in Java (1).pdfHow to Read Excel Files in Java (1).pdf
How to Read Excel Files in Java (1).pdf
SudhanshiBakre1
 
Exploiting JXL using Selenium
Exploiting JXL using SeleniumExploiting JXL using Selenium
Exploiting JXL using Selenium
OSSCube
 
Using existing language skillsets to create large-scale, cloud-based analytics
Using existing language skillsets to create large-scale, cloud-based analyticsUsing existing language skillsets to create large-scale, cloud-based analytics
Using existing language skillsets to create large-scale, cloud-based analytics
Microsoft Tech Community
 
Advancing JavaScript with Libraries (Yahoo Tech Talk)
Advancing JavaScript with Libraries (Yahoo Tech Talk)Advancing JavaScript with Libraries (Yahoo Tech Talk)
Advancing JavaScript with Libraries (Yahoo Tech Talk)
jeresig
 
IOstreams hgjhsgfdjyggckhgckhjxfhbuvobunciu
IOstreams hgjhsgfdjyggckhgckhjxfhbuvobunciuIOstreams hgjhsgfdjyggckhgckhjxfhbuvobunciu
IOstreams hgjhsgfdjyggckhgckhjxfhbuvobunciu
akinbhattarai1
 
Utilizing the open ntf domino api
Utilizing the open ntf domino apiUtilizing the open ntf domino api
Utilizing the open ntf domino api
Oliver Busse
 
CSE3146-ADV JAVA M2.pdf
CSE3146-ADV JAVA M2.pdfCSE3146-ADV JAVA M2.pdf
CSE3146-ADV JAVA M2.pdf
VithalReddy3
 
Monhocvecaujahetvagiuplaptunhhayhonha.pdf
Monhocvecaujahetvagiuplaptunhhayhonha.pdfMonhocvecaujahetvagiuplaptunhhayhonha.pdf
Monhocvecaujahetvagiuplaptunhhayhonha.pdf
cuchuoi83ne
 
Java Input Output and File Handling
Java Input Output and File HandlingJava Input Output and File Handling
Java Input Output and File Handling
Sunil OS
 
Data file handling
Data file handlingData file handling
Data file handling
Saurabh Patel
 
Input output files in java
Input output files in javaInput output files in java
Input output files in java
Kavitha713564
 
20483B_061203forITVietNamNumber12025.pptx
20483B_061203forITVietNamNumber12025.pptx20483B_061203forITVietNamNumber12025.pptx
20483B_061203forITVietNamNumber12025.pptx
humiss545
 
Java I/O
Java I/OJava I/O
Java I/O
Jayant Dalvi
 
20120601_Excel 元件 ep plus joncash
20120601_Excel 元件 ep plus joncash20120601_Excel 元件 ep plus joncash
20120601_Excel 元件 ep plus joncash
LearningTech
 
IOStream.pptx
IOStream.pptxIOStream.pptx
IOStream.pptx
HindAlmisbahi
 
Tanel Poder - Scripts and Tools short
Tanel Poder - Scripts and Tools shortTanel Poder - Scripts and Tools short
Tanel Poder - Scripts and Tools short
Tanel Poder
 
Java I/O
Java I/OJava I/O
Java I/O
Jussi Pohjolainen
 
Utilizing the OpenNTF Domino API
Utilizing the OpenNTF Domino APIUtilizing the OpenNTF Domino API
Utilizing the OpenNTF Domino API
Oliver Busse
 
5java Io
5java Io5java Io
5java Io
Adil Jafri
 
Systemcall1
Systemcall1Systemcall1
Systemcall1
pavimalpani
 
How to Read Excel Files in Java (1).pdf
How to Read Excel Files in Java (1).pdfHow to Read Excel Files in Java (1).pdf
How to Read Excel Files in Java (1).pdf
SudhanshiBakre1
 
Exploiting JXL using Selenium
Exploiting JXL using SeleniumExploiting JXL using Selenium
Exploiting JXL using Selenium
OSSCube
 
Using existing language skillsets to create large-scale, cloud-based analytics
Using existing language skillsets to create large-scale, cloud-based analyticsUsing existing language skillsets to create large-scale, cloud-based analytics
Using existing language skillsets to create large-scale, cloud-based analytics
Microsoft Tech Community
 
Advancing JavaScript with Libraries (Yahoo Tech Talk)
Advancing JavaScript with Libraries (Yahoo Tech Talk)Advancing JavaScript with Libraries (Yahoo Tech Talk)
Advancing JavaScript with Libraries (Yahoo Tech Talk)
jeresig
 
IOstreams hgjhsgfdjyggckhgckhjxfhbuvobunciu
IOstreams hgjhsgfdjyggckhgckhjxfhbuvobunciuIOstreams hgjhsgfdjyggckhgckhjxfhbuvobunciu
IOstreams hgjhsgfdjyggckhgckhjxfhbuvobunciu
akinbhattarai1
 
Utilizing the open ntf domino api
Utilizing the open ntf domino apiUtilizing the open ntf domino api
Utilizing the open ntf domino api
Oliver Busse
 
CSE3146-ADV JAVA M2.pdf
CSE3146-ADV JAVA M2.pdfCSE3146-ADV JAVA M2.pdf
CSE3146-ADV JAVA M2.pdf
VithalReddy3
 
Monhocvecaujahetvagiuplaptunhhayhonha.pdf
Monhocvecaujahetvagiuplaptunhhayhonha.pdfMonhocvecaujahetvagiuplaptunhhayhonha.pdf
Monhocvecaujahetvagiuplaptunhhayhonha.pdf
cuchuoi83ne
 
Java Input Output and File Handling
Java Input Output and File HandlingJava Input Output and File Handling
Java Input Output and File Handling
Sunil OS
 
Input output files in java
Input output files in javaInput output files in java
Input output files in java
Kavitha713564
 
20483B_061203forITVietNamNumber12025.pptx
20483B_061203forITVietNamNumber12025.pptx20483B_061203forITVietNamNumber12025.pptx
20483B_061203forITVietNamNumber12025.pptx
humiss545
 
20120601_Excel 元件 ep plus joncash
20120601_Excel 元件 ep plus joncash20120601_Excel 元件 ep plus joncash
20120601_Excel 元件 ep plus joncash
LearningTech
 
Tanel Poder - Scripts and Tools short
Tanel Poder - Scripts and Tools shortTanel Poder - Scripts and Tools short
Tanel Poder - Scripts and Tools short
Tanel Poder
 
Utilizing the OpenNTF Domino API
Utilizing the OpenNTF Domino APIUtilizing the OpenNTF Domino API
Utilizing the OpenNTF Domino API
Oliver Busse
 
Ad

More from Srikrishna k (16)

Android
AndroidAndroid
Android
Srikrishna k
 
Hsqldb tutorial
Hsqldb tutorialHsqldb tutorial
Hsqldb tutorial
Srikrishna k
 
S3inmule
S3inmuleS3inmule
S3inmule
Srikrishna k
 
Mule sqs
Mule sqsMule sqs
Mule sqs
Srikrishna k
 
Introduction testingmule
Introduction testingmuleIntroduction testingmule
Introduction testingmule
Srikrishna k
 
Designpattern
DesignpatternDesignpattern
Designpattern
Srikrishna k
 
Java util
Java utilJava util
Java util
Srikrishna k
 
Kafka tutorial
Kafka tutorialKafka tutorial
Kafka tutorial
Srikrishna k
 
Test ng tutorial
Test ng tutorialTest ng tutorial
Test ng tutorial
Srikrishna k
 
Webservices intro
Webservices introWebservices intro
Webservices intro
Srikrishna k
 
Easy mock
Easy mockEasy mock
Easy mock
Srikrishna k
 
Apache kafka
Apache kafkaApache kafka
Apache kafka
Srikrishna k
 
Apachespark 160612140708
Apachespark 160612140708Apachespark 160612140708
Apachespark 160612140708
Srikrishna k
 
Vmtransport 160723040146
Vmtransport 160723040146Vmtransport 160723040146
Vmtransport 160723040146
Srikrishna k
 
Groovydemo 160721051742
Groovydemo 160721051742Groovydemo 160721051742
Groovydemo 160721051742
Srikrishna k
 
Apache kafka
Apache kafkaApache kafka
Apache kafka
Srikrishna k
 
Ad

Recently uploaded (20)

How to Install and Activate ListGrabber Plugin
How to Install and Activate ListGrabber PluginHow to Install and Activate ListGrabber Plugin
How to Install and Activate ListGrabber Plugin
eGrabber
 
How I solved production issues with OpenTelemetry
How I solved production issues with OpenTelemetryHow I solved production issues with OpenTelemetry
How I solved production issues with OpenTelemetry
Cees Bos
 
UI/UX Design & Development and Servicess
UI/UX Design & Development and ServicessUI/UX Design & Development and Servicess
UI/UX Design & Development and Servicess
marketing810348
 
How to Create a Crypto Wallet Like Trust.pptx
How to Create a Crypto Wallet Like Trust.pptxHow to Create a Crypto Wallet Like Trust.pptx
How to Create a Crypto Wallet Like Trust.pptx
riyageorge2024
 
Do not let staffing shortages and limited fiscal view hamper your cause
Do not let staffing shortages and limited fiscal view hamper your causeDo not let staffing shortages and limited fiscal view hamper your cause
Do not let staffing shortages and limited fiscal view hamper your cause
Fexle Services Pvt. Ltd.
 
Let's Do Bad Things to Unsecured Containers
Let's Do Bad Things to Unsecured ContainersLet's Do Bad Things to Unsecured Containers
Let's Do Bad Things to Unsecured Containers
Gene Gotimer
 
Solar-wind hybrid engery a system sustainable power
Solar-wind  hybrid engery a system sustainable powerSolar-wind  hybrid engery a system sustainable power
Solar-wind hybrid engery a system sustainable power
bhoomigowda12345
 
S3 + AWS Athena how to integrate s3 aws plus athena
S3 + AWS Athena how to integrate s3 aws plus athenaS3 + AWS Athena how to integrate s3 aws plus athena
S3 + AWS Athena how to integrate s3 aws plus athena
aianand98
 
Reinventing Microservices Efficiency and Innovation with Single-Runtime
Reinventing Microservices Efficiency and Innovation with Single-RuntimeReinventing Microservices Efficiency and Innovation with Single-Runtime
Reinventing Microservices Efficiency and Innovation with Single-Runtime
Natan Silnitsky
 
Artificial hand using embedded system.pptx
Artificial hand using embedded system.pptxArtificial hand using embedded system.pptx
Artificial hand using embedded system.pptx
bhoomigowda12345
 
Welcome to QA Summit 2025.
Welcome to QA Summit 2025.Welcome to QA Summit 2025.
Welcome to QA Summit 2025.
QA Summit
 
Download 4k Video Downloader Crack Pre-Activated
Download 4k Video Downloader Crack Pre-ActivatedDownload 4k Video Downloader Crack Pre-Activated
Download 4k Video Downloader Crack Pre-Activated
Web Designer
 
!%& IDM Crack with Internet Download Manager 6.42 Build 32 >
!%& IDM Crack with Internet Download Manager 6.42 Build 32 >!%& IDM Crack with Internet Download Manager 6.42 Build 32 >
!%& IDM Crack with Internet Download Manager 6.42 Build 32 >
Ranking Google
 
The-Future-is-Hybrid-Exploring-Azure’s-Role-in-Multi-Cloud-Strategies.pptx
The-Future-is-Hybrid-Exploring-Azure’s-Role-in-Multi-Cloud-Strategies.pptxThe-Future-is-Hybrid-Exploring-Azure’s-Role-in-Multi-Cloud-Strategies.pptx
The-Future-is-Hybrid-Exploring-Azure’s-Role-in-Multi-Cloud-Strategies.pptx
james brownuae
 
Buy vs. Build: Unlocking the right path for your training tech
Buy vs. Build: Unlocking the right path for your training techBuy vs. Build: Unlocking the right path for your training tech
Buy vs. Build: Unlocking the right path for your training tech
Rustici Software
 
cram_advancedword2007version2025final.ppt
cram_advancedword2007version2025final.pptcram_advancedword2007version2025final.ppt
cram_advancedword2007version2025final.ppt
ahmedsaadtax2025
 
Why CoTester Is the AI Testing Tool QA Teams Can’t Ignore
Why CoTester Is the AI Testing Tool QA Teams Can’t IgnoreWhy CoTester Is the AI Testing Tool QA Teams Can’t Ignore
Why CoTester Is the AI Testing Tool QA Teams Can’t Ignore
Shubham Joshi
 
wAIred_LearnWithOutAI_JCON_14052025.pptx
wAIred_LearnWithOutAI_JCON_14052025.pptxwAIred_LearnWithOutAI_JCON_14052025.pptx
wAIred_LearnWithOutAI_JCON_14052025.pptx
SimonedeGijt
 
Serato DJ Pro Crack Latest Version 2025??
Serato DJ Pro Crack Latest Version 2025??Serato DJ Pro Crack Latest Version 2025??
Serato DJ Pro Crack Latest Version 2025??
Web Designer
 
Wilcom Embroidery Studio Crack Free Latest 2025
Wilcom Embroidery Studio Crack Free Latest 2025Wilcom Embroidery Studio Crack Free Latest 2025
Wilcom Embroidery Studio Crack Free Latest 2025
Web Designer
 
How to Install and Activate ListGrabber Plugin
How to Install and Activate ListGrabber PluginHow to Install and Activate ListGrabber Plugin
How to Install and Activate ListGrabber Plugin
eGrabber
 
How I solved production issues with OpenTelemetry
How I solved production issues with OpenTelemetryHow I solved production issues with OpenTelemetry
How I solved production issues with OpenTelemetry
Cees Bos
 
UI/UX Design & Development and Servicess
UI/UX Design & Development and ServicessUI/UX Design & Development and Servicess
UI/UX Design & Development and Servicess
marketing810348
 
How to Create a Crypto Wallet Like Trust.pptx
How to Create a Crypto Wallet Like Trust.pptxHow to Create a Crypto Wallet Like Trust.pptx
How to Create a Crypto Wallet Like Trust.pptx
riyageorge2024
 
Do not let staffing shortages and limited fiscal view hamper your cause
Do not let staffing shortages and limited fiscal view hamper your causeDo not let staffing shortages and limited fiscal view hamper your cause
Do not let staffing shortages and limited fiscal view hamper your cause
Fexle Services Pvt. Ltd.
 
Let's Do Bad Things to Unsecured Containers
Let's Do Bad Things to Unsecured ContainersLet's Do Bad Things to Unsecured Containers
Let's Do Bad Things to Unsecured Containers
Gene Gotimer
 
Solar-wind hybrid engery a system sustainable power
Solar-wind  hybrid engery a system sustainable powerSolar-wind  hybrid engery a system sustainable power
Solar-wind hybrid engery a system sustainable power
bhoomigowda12345
 
S3 + AWS Athena how to integrate s3 aws plus athena
S3 + AWS Athena how to integrate s3 aws plus athenaS3 + AWS Athena how to integrate s3 aws plus athena
S3 + AWS Athena how to integrate s3 aws plus athena
aianand98
 
Reinventing Microservices Efficiency and Innovation with Single-Runtime
Reinventing Microservices Efficiency and Innovation with Single-RuntimeReinventing Microservices Efficiency and Innovation with Single-Runtime
Reinventing Microservices Efficiency and Innovation with Single-Runtime
Natan Silnitsky
 
Artificial hand using embedded system.pptx
Artificial hand using embedded system.pptxArtificial hand using embedded system.pptx
Artificial hand using embedded system.pptx
bhoomigowda12345
 
Welcome to QA Summit 2025.
Welcome to QA Summit 2025.Welcome to QA Summit 2025.
Welcome to QA Summit 2025.
QA Summit
 
Download 4k Video Downloader Crack Pre-Activated
Download 4k Video Downloader Crack Pre-ActivatedDownload 4k Video Downloader Crack Pre-Activated
Download 4k Video Downloader Crack Pre-Activated
Web Designer
 
!%& IDM Crack with Internet Download Manager 6.42 Build 32 >
!%& IDM Crack with Internet Download Manager 6.42 Build 32 >!%& IDM Crack with Internet Download Manager 6.42 Build 32 >
!%& IDM Crack with Internet Download Manager 6.42 Build 32 >
Ranking Google
 
The-Future-is-Hybrid-Exploring-Azure’s-Role-in-Multi-Cloud-Strategies.pptx
The-Future-is-Hybrid-Exploring-Azure’s-Role-in-Multi-Cloud-Strategies.pptxThe-Future-is-Hybrid-Exploring-Azure’s-Role-in-Multi-Cloud-Strategies.pptx
The-Future-is-Hybrid-Exploring-Azure’s-Role-in-Multi-Cloud-Strategies.pptx
james brownuae
 
Buy vs. Build: Unlocking the right path for your training tech
Buy vs. Build: Unlocking the right path for your training techBuy vs. Build: Unlocking the right path for your training tech
Buy vs. Build: Unlocking the right path for your training tech
Rustici Software
 
cram_advancedword2007version2025final.ppt
cram_advancedword2007version2025final.pptcram_advancedword2007version2025final.ppt
cram_advancedword2007version2025final.ppt
ahmedsaadtax2025
 
Why CoTester Is the AI Testing Tool QA Teams Can’t Ignore
Why CoTester Is the AI Testing Tool QA Teams Can’t IgnoreWhy CoTester Is the AI Testing Tool QA Teams Can’t Ignore
Why CoTester Is the AI Testing Tool QA Teams Can’t Ignore
Shubham Joshi
 
wAIred_LearnWithOutAI_JCON_14052025.pptx
wAIred_LearnWithOutAI_JCON_14052025.pptxwAIred_LearnWithOutAI_JCON_14052025.pptx
wAIred_LearnWithOutAI_JCON_14052025.pptx
SimonedeGijt
 
Serato DJ Pro Crack Latest Version 2025??
Serato DJ Pro Crack Latest Version 2025??Serato DJ Pro Crack Latest Version 2025??
Serato DJ Pro Crack Latest Version 2025??
Web Designer
 
Wilcom Embroidery Studio Crack Free Latest 2025
Wilcom Embroidery Studio Crack Free Latest 2025Wilcom Embroidery Studio Crack Free Latest 2025
Wilcom Embroidery Studio Crack Free Latest 2025
Web Designer
 

Apachepoitutorial

  • 1. Apache POI Tutorial Apache POI is a popular API that allows programmers to create, modify, and display MS Office files using Java programs. It is an open source library developed and distributed by Apache Software Foundation to design or modify Microsoft Office files using Java program. The name POI was originally an acronym for Poor Obfuscation Implementation, referring humorously to the fact that the file formats seemed to be deliberately obfuscated, but poorly, since they were successfully reverse-engineered.
  • 2. • HSSF (Horrible SpreadSheet Format) – reads and writes Microsoft Excel (XLS) format files. • XSSF (XML SpreadSheet Format) – reads and writes Office Open XML (XLSX) format files. • HPSF (Horrible Property Set Format) – reads “Document Summary” information from Microsoft Office files. • HWPF (Horrible Word Processor Format) – aims to read and write Microsoft Word 97 (DOC) format files. • HSLF (Horrible Slide Layout Format) – a pure Java implementation for Microsoft PowerPoint files. • HDGF (Horrible DiaGram Format) – an initial pure Java implementation for Microsoft Visio binary files. • HPBF (Horrible PuBlisher Format) – a pure Java implementation for Microsoft Publisher files. • HSMF (Horrible Stupid Mail Format) – a pure Java implementation for Microsoft Outlook MSG files • DDF (Dreadful Drawing Format) – a package for decoding the Microsoft Office Drawing format.
  • 3. • Working with .xlsx files • The classes we used in above code snippet, HSSFWorkbook and HSSFSheet works for .xls format. In order to work with newer xls format viz .xlsx, you need to see newer POI classes like: • import org.apache.poi.hssf.usermodel.HSSFSheet; • import org.apache.poi.xssf.usermodel.XSSFSheet; • FileInputStream file = new FileInputStream(new File("C:test.xlsx")); • //Get the workbook instance for XLS file • XSSFWorkbook workbook = new XSSFWorkbook (file); • //Get first sheet from the workbook XSSFSheet sheet = workbook.getSheetAt(0); • //Get iterator to all the rows in current sheet • Iterator<Row> rowIterator = sheet.iterator(); • //Get iterator to all cells of current row • Iterator<Cell> cellIterator = row.cellIterator();
  • 4. Create Blank Workbook • The following simple program is used to create a blank Microsoft Excel Workbook. • import java.io.*; import org.apache.poi.xssf.usermodel.*; • public class CreateWorkBook { • public static void main(String[] args)throws Exception { • //Create Blank workbook • XSSFWorkbook workbook = new XSSFWorkbook(); //Create file system using specific name • FileOutputStream out = new FileOutputStream( new File("createworkbook.xlsx")); • //write operation workbook using file out object workbook.write(out); out.close(); System.out.println(" createworkbook.xlsx written successfully"); • } }
  • 5. Open Existing Workbook • Use the following code to open an existing workbook. • import java.io.*; import org.apache.poi.xssf.usermodel.*; • public class OpenWorkBook { • public static void main(String args[])throws Exception { File file = new File("openworkbook.xlsx"); FileInputStream fIP = new FileInputStream(file); • //Get the workbook instance for XLSX file • XSSFWorkbook workbook = new XSSFWorkbook(fIP); if(file.isFile() && file.exists()) • { System.out.println( "openworkbook.xlsx file open successfully."); • } else { System.out.println( "Error to open openworkbook.xlsx file."); } } }
  • 6. We will read above xls file using Apache POI and prints the data. • Consider a sample excel file: • test.xls
  • 7. • try { • • FileInputStream file = new FileInputStream(new File("C:test.xls")); • • //Get the workbook instance for XLS file • HSSFWorkbook workbook = new HSSFWorkbook(file); • //Get first sheet from the workbook • HSSFSheet sheet = workbook.getSheetAt(0); • • //Iterate through each rows from first sheet • Iterator<Row> rowIterator = sheet.iterator(); • while(rowIterator.hasNext()) { • Row row = rowIterator.next(); • • //For each row, iterate through each columns • Iterator<Cell> cellIterator = row.cellIterator(); • while(cellIterator.hasNext()) { • • Cell cell = cellIterator.next();
  • 8. • switch(cell.getCellType()) { • case Cell.CELL_TYPE_BOOLEAN: • System.out.print(cell.getBooleanCellValue() + "tt"); • break; • case Cell.CELL_TYPE_NUMERIC: • System.out.print(cell.getNumericCellValue() + "tt"); • break; • case Cell.CELL_TYPE_STRING: • System.out.print(cell.getStringCellValue() + "tt"); • break; • } • } • System.out.println(""); • }
  • 9. • file.close(); • FileOutputStream out = • new FileOutputStream(new File("C:test.xls")); • workbook.write(out); • out.close(); • • } catch (FileNotFoundException e) { • e.printStackTrace(); • } catch (IOException e) { • e.printStackTrace(); • }
  • 10. • 3. Create New Excel File • Let us create a new excel file and write data in it. Following is the API which we will use for this purpose. • import org.apache.poi.hssf.usermodel.HSSFSheet; • import org.apache.poi.hssf.usermodel.HSSFWorkbook; • //.. • HSSFWorkbook workbook = new HSSFWorkbook(); • HSSFSheet sheet = workbook.createSheet("Sample sheet"); • //Create a new row in current sheet • Row row = sheet.createRow(0); • //Create a new cell in current row • Cell cell = row.createCell(0); • //Set value to new value • cell.setCellValue("Blahblah");
  • 11. • Below is the complete code that writes a new excel with dummy data: • HSSFWorkbook workbook = new HSSFWorkbook(); • HSSFSheet sheet = workbook.createSheet("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}); • • Set<String> keyset = data.keySet(); • int rownum = 0; • for (String key : keyset) { • Row row = sheet.createRow(rownum++); • Object [] objArr = data.get(key); • int cellnum = 0; • for (Object obj : objArr) { • Cell cell = row.createCell(cellnum++); • if(obj instanceof Date) • cell.setCellValue((Date)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); • } • } •
  • 12. • try { • FileOutputStream out = • new FileOutputStream(new File("C:new.xls")); • workbook.write(out); • out.close(); • System.out.println("Excel written successfully.."); • • } catch (FileNotFoundException e) { • e.printStackTrace(); • } catch (IOException e) { • e.printStackTrace(); • }
  翻译: