JExcelApi is a Java
library that is dedicated for reading, writing and modifying Excel
spreadsheets. It supports Excel 2003 file format and older versions. You can
download JExcelApi from the following link:
To work with JExcelApi,
you need to add its only jar file: jxl.jar - to your project’s classpath. Download JExcel
Here we are going to create
spring API to download Excel file as response using JExcel Api.
import
javax.servlet.http.HttpServletResponse;
import
org.springframework.beans.factory.annotation.Autowired;
import
org.springframework.web.bind.annotation.RequestMapping;
import
org.springframework.web.bind.annotation.RequestMethod;
import
org.springframework.web.bind.annotation.RestController;
import
org.springframework.web.servlet.ModelAndView;
import
com.demo.api.exl.services.ExcelOutputService;
@RestController
@RequestMapping(value="/exceloutput")
public class ExcelOutputServiceController
{
@Autowired
ExcelOutputService excelOutputService;
@RequestMapping(value="/download",
method=RequestMethod.GET)
public ModelAndView
downloadExcelOutputExl(HttpServletResponse response){
excelOutputService.createExcelOutputExcel(response);
return null;
}
}
import
javax.servlet.http.HttpServletResponse;
import jxl.Workbook;
import jxl.write.Label;
import
jxl.write.WritableSheet;
import
jxl.write.WritableWorkbook;
import
jxl.write.WriteException;
import
jxl.write.biff.RowsExceededException;
import
org.apache.log4j.Logger;
import
org.springframework.beans.factory.annotation.Autowired;
import
org.springframework.stereotype.Service;
@Service("excelOutputService")
public class ExcelOutputServiceImpl implements ExcelOutputService{
private static final Logger LOGGER = Logger.getLogger(ExcelOutputServiceImpl.class);
@Override
public WritableWorkbook
createExcelOutputExcel(HttpServletResponse response) {
String
fileName = "Excel_Output.xls";
WritableWorkbook
writableWorkbook = null;
try {
response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-Disposition", "attachment;
filename=" + fileName);
writableWorkbook = Workbook.createWorkbook(response.getOutputStream());
WritableSheet excelOutputsheet = writableWorkbook.createSheet("Excel Output", 0);
addExcelOutputHeader(excelOutputsheet);
writeExcelOutputData(excelOutputsheet);
writableWorkbook.write();
writableWorkbook.close();
}
catch (Exception e) {
LOGGER.error("Error occured
while creating Excel file", e);
}
return writableWorkbook;
}
private void addExcelOutputHeader(WritableSheet
sheet) throws
RowsExceededException, WriteException{
// create header
row
sheet.addCell(new Label(0, 0, "Column 1"));
sheet.addCell(new Label(1, 0, " Column 2"));
sheet.addCell(new Label(2, 0, " Column 3"));
sheet.addCell(new Label(3, 0, " Column 4"));
sheet.addCell(new Label(4, 0, " Column 5"));
sheet.addCell(new Label(5, 0, " Column 6"));
sheet.addCell(new Label(6, 0, " Column 7"));
sheet.addCell(new Label(7, 0, " Column 8"));
sheet.addCell(new Label(8, 0, " Column 9"));
sheet.addCell(new Label(9, 0, " Column 10"));
sheet.addCell(new Label(10, 0, " Column 11"));
}
private void writeExcelOutputData(WritableSheet
sheet) throws
RowsExceededException, WriteException{
for(int
rowNo = 1; rowNo<=10; rowNo++){
sheet.addCell(new Label(0, rowNo, “Col Data ”+ (rowNo+0)));
sheet.addCell(new Label(1, rowNo, “Col Data ”+ (rowNo+1)));
sheet.addCell(new Label(2, rowNo, “Col Data ”+ (rowNo+2)));
sheet.addCell(new Label(3, rowNo, “Col Data ”+ (rowNo+3)));
sheet.addCell(new Label(4, rowNo, “Col Data ”+ (rowNo+4)));
sheet.addCell(new Label(5, rowNo, “Col Data ”+ (rowNo+5)));
sheet.addCell(new Label(6, rowNo, “Col Data ”+ (rowNo+6)));
sheet.addCell(new Label(7, rowNo, “Col Data ”+ (rowNo+7)));
sheet.addCell(new Label(8, rowNo, “Col Data ”+ (rowNo+8)));
sheet.addCell(new Label(9, rowNo, “Col Data ”+ (rowNo+9)));
sheet.addCell(new Label(10, rowNo, “Col Data ”+ (rowNo+10)));
}
}
}
Now call the service as :
http://localhost:8080/demo/excelOutputService/download
This will download the
Excel file to download folder or show download popup window.Create Excel using Apache POI
POI-XSSF - Java
API To Access Microsoft Excel Format Files
Apache POI is a
popular API that allows programmers to create, modify, and display MS Office
files using Java programs.
XSSF is the POI Project's pure Java implementation of the Excel 2007 OOXML (.xlsx) file format.
XSSF is the POI Project's pure Java implementation of the Excel 2007 OOXML (.xlsx) file format.
Prerequisites
·
DK1.5 or later versions
·
Apache POI library (http://poi.apache.org/download.html )
Add following
library to classpath:
“C:\poi-3.9\poi-3.9-20121203.jar;”
“C:\poi-3.9\poi-ooxml-3.9-20121203.jar;”
“C:\poi-3.9\poi-ooxml-schemas-3.9-20121203.jar;”
“C:\poi-3.9\ooxml-lib\dom4j-1.6.1.jar;”
“C:\poi-3.9\ooxml-lib\xmlbeans-2.3.0.jar;.;”
import org.apache.log4j.Logger;
import
org.apache.poi.xssf.streaming.SXSSFWorkbook;
import
org.apache.poi.xssf.usermodel.XSSFCell;
import
org.apache.poi.xssf.usermodel.XSSFCellStyle;
import
org.apache.poi.xssf.usermodel.XSSFRow;
import
org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public XSSFWorkbook createExcelOutputFile(){
XSSFWorkbook workbook = null;
try {
ClassLoader loader = getClass().getClassLoader();
File file = new File(loader.getResource("Excel_Output.xlsx").getFile()); //file should
be at classpath
FileInputStream is = new FileInputStream(file);
// Get the
workbook instance for XLSX file
workbook = new XSSFWorkbook(is);
XSSFSheet rankerSheet1 = workbook.getSheetAt(0);
writeExcelOutputData(rankerSheet1, workbook);
is.close();
} catch (FileNotFoundException e) {
LOGGER.error(e);
} catch (IOException e) {
LOGGER.error(e);
}
return workbook;
}
private void writeExcelOutputData(XSSFSheet rankerSheet, XSSFWorkbook worksheet){
XSSFRow row1 = rankerSheet.createRow(1);
row1.createCell(0).setCellValue(1);
row1.createCell(1).setCellValue(1234
row1.createCell(2).setCellValue(“Test Excel”);
row1.createCell(3).setCellValue(“Address”);
XSSFCell cell4 = row1.createCell(4);
cell4.setCellValue(10.00);
XSSFCellStyle style = worksheet.createCellStyle();
style.setDataFormat((short)8); // this will format cell with $ sign Ex: $10.00
cell4.setCellStyle(style);
//Creating Data format %
XSSFCellStyle percentStyle = worksheet.createCellStyle();
percentStyle.setDataFormat(worksheet.createDataFormat().getFormat("0.0%"));
XSSFCell cell5 = row1.createCell(5);
cell5.setCellStyle(percentStyle);
cell5.setCellValue(20.00);
}
Excellent POST.
ReplyDeleteGive a Complete flow with excel / pdf / image UPLOAD with Spring REST.
ReplyDeleteAshis,
ReplyDeleteyou can follow the link http://simplejava2.blogspot.in/2016/06/image-uplaod-using-spring-restful.html
for upload a file using spring freamework.
Hi Can you Please Share the Following Code Base (ie Using Apche POI)
ReplyDeleteHi Can you Please Share the Following Code Base (ie Using Apche POI)
ReplyDeleteI have read your blog its very attractive and impressive. I like it your blog.
ReplyDeleteSpring online training Java Web Services Training Online
Java Web Services Training spring training in chennai
Hi, Could you please share me example to export jsp table into pdf,excel using spring rest service
ReplyDeleteAjudou muito :)
ReplyDeleteIt is better to engaged ourselves in activities we like. I liked the post. Thanks for sharing.
ReplyDeleteSelenium training in Chennai
Selenium training in Bangalore
Thanks for taking time to share this valuable information admin. Really helpful.
ReplyDeletePython Training in Chennai
Python Classes in Chennai
ccna Training institute in Chennai
ccna institute in Chennai
Data Science Course in Chennai
DevOps Training in Chennai
Python Training in T Nagar
Python Training in Adyar
This blog is the general information for the feature. You got a good work for these blog.We have a developing our creative content of this mind.Thank you for this blog. This for very interesting and useful.
ReplyDeleteDevops Training in Bangalore
Microsoft azure training in Bangalore
Power bi training in Chennai
I found your blog while searching for the updates, I am happy to be here. Very useful content and also easily understandable providing..
ReplyDeleteBelieve me I did wrote an post about tutorials for beginners with reference of your blog.
Selenium training in bangalore
Selenium training in Chennai
Selenium training in Bangalore
Selenium training in Pune
Selenium Online training
Thank you for taking the time and sharing this information with us. It was indeed very helpful and insightful while being straight forward and to the point.
ReplyDeleteData Science Training in Indira nagar
Data Science Training in btm layout
Data Science Training in Kalyan nagar
Data Science training in Indira nagar
Data science training in bangalore
This comment has been removed by the author.
ReplyDeleteThis comment has been removed by the author.
ReplyDeleteI think you need to put forward a good amount of informative and descriptive article for learning every tact of REST API.
ReplyDeletePower BI Read Rest
Informative post indeed, I’ve being in and out reading posts regularly and I see alot of engaging people sharing things and majority of the shared information is very valuable and so, here’s my fine read.
ReplyDeleteclick here for registration
click here full resolution
click here for result
click here for b.pharmacy examinations results
click here for udyog aadhaar registration certificate
The blog is nicely maintained.its good for study.keep sharing like this.good job.
ReplyDeleteSalesforce Training | Online Course | Certification in chennai | Salesforce Training | Online Course | Certification in bangalore | Salesforce Training | Online Course | Certification in hyderabad | Salesforce Training | Online Course | Certification in pune
Nice Post. the post is really very impressive, every concepts are uniquely represented.
ReplyDeleteData Science Training Course In Chennai | Data Science Training Course In Anna Nagar | Data Science Training Course In OMR | Data Science Training Course In Porur | Data Science Training Course In Tambaram | Data Science Training Course In Velachery
Great Article… I love to read your articles because your writing style is too good, its is very very helpful for all of us and I never get bored while reading your article because, they are becomes a more and more interesting from the starting lines until the end.
ReplyDeleteSalesforce Training in Chennai | Certification | Online Course | Salesforce Training in Bangalore | Certification | Online Course | Salesforce Training in Hyderabad | Certification | Online Course | Salesforce Training in Pune | Certification | Online Course | Salesforce Online Training | Salesforce Training
I simply wanted to write down a quick word to say thanks to you for those wonderful tips and hints you are showing on this site.
ReplyDeleteCyber Security Training Course in Chennai | Certification | Cyber Security Online Training Course | Ethical Hacking Training Course in Chennai | Certification | Ethical Hacking Online Training Course |
CCNA Training Course in Chennai | Certification | CCNA Online Training Course | RPA Robotic Process Automation Training Course in Chennai | Certification | RPA Training Course Chennai | SEO Training in Chennai | Certification | SEO Online Training Course
This post is so interactive and informative.keep update more information...
ReplyDeleteRPA Training in Tambaram
RPA Training in Chennai
This post is so interactive and informative.keep update more information…
ReplyDeleteSEO Training in Anna Nagar
SEO Training in Chennai
perde modelleri
ReplyDeleteNumara onay
mobil ödeme bozdurma
nft nasıl alınır
ANKARA EVDEN EVE NAKLİYAT
TRAFİK SİGORTASİ
Dedektor
web sitesi kurma
Aşk Kitapları
canlı sex hattı
ReplyDeleteheets
salt likit
salt likit
puff bar
61TR1F
canlı sex hattı
ReplyDeleteheets
salt likit
salt likit
puff bar
SNCİ
malatya
ReplyDeleteelazığ
kadıköy
istanbul
şişli
8ROM
malatya
ReplyDeleteelazığ
kadıköy
istanbul
şişli
SSH
salt likit
ReplyDeletesalt likit
dr mood likit
big boss likit
dl likit
dark likit
YNHS
https://saglamproxy.com
ReplyDeletemetin2 proxy
proxy satın al
knight online proxy
mobil proxy satın al
FD4J
https://saglamproxy.com
ReplyDeletemetin2 proxy
proxy satın al
knight online proxy
mobil proxy satın al
2SFQJ
Adana
ReplyDeleteErzurum
Sinop
istanbul
Düzce
1QL
tekirdağ
ReplyDeletetokat
elazığ
adıyaman
çankırı
U5GL3
van
ReplyDeleteelazığ
zonguldak
uşak
sakarya
KOK0KY
ankara parça eşya taşıma
ReplyDeletetakipçi satın al
antalya rent a car
antalya rent a car
ankara parça eşya taşıma
D7VYA
href="https://istanbulolala.biz/">https://istanbulolala.biz/
ReplyDelete8ZT
ığdır evden eve nakliyat
ReplyDeletebitlis evden eve nakliyat
batman evden eve nakliyat
rize evden eve nakliyat
niğde evden eve nakliyat
X85
karabük evden eve nakliyat
ReplyDeletebartın evden eve nakliyat
maraş evden eve nakliyat
mersin evden eve nakliyat
aksaray evden eve nakliyat
25VO
urfa evden eve nakliyat
ReplyDeletemalatya evden eve nakliyat
burdur evden eve nakliyat
kırıkkale evden eve nakliyat
kars evden eve nakliyat
NP7
3B156
ReplyDeleteYozgat Evden Eve Nakliyat
Tekirdağ Şehirler Arası Nakliyat
Bursa Evden Eve Nakliyat
Hakkari Şehir İçi Nakliyat
Tunceli Şehirler Arası Nakliyat
Ankara Asansör Tamiri
Trabzon Lojistik
Yenimahalle Fayans Ustası
Bingöl Parça Eşya Taşıma
A2744
ReplyDeleteBitcoin Kazanma
Karaman Şehirler Arası Nakliyat
Etimesgut Fayans Ustası
Çerkezköy Mutfak Dolabı
Rize Lojistik
Zonguldak Lojistik
Burdur Şehirler Arası Nakliyat
Binance Güvenilir mi
Xcn Coin Hangi Borsada
1A1CB
ReplyDeleteBingöl Şehir İçi Nakliyat
Bartın Şehirler Arası Nakliyat
Luffy Coin Hangi Borsada
Tekirdağ Parça Eşya Taşıma
Ankara Boya Ustası
Aydın Parça Eşya Taşıma
Çerkezköy Parke Ustası
Pursaklar Parke Ustası
Ağrı Parça Eşya Taşıma
5C786
ReplyDeleteCoin Nedir
Burdur Evden Eve Nakliyat
Keçiören Boya Ustası
Pursaklar Parke Ustası
Maraş Evden Eve Nakliyat
Tokat Evden Eve Nakliyat
Mardin Evden Eve Nakliyat
Zonguldak Evden Eve Nakliyat
Bayburt Evden Eve Nakliyat
9852B
ReplyDeleteEdirne Şehir İçi Nakliyat
Tekirdağ Lojistik
buy testosterone enanthate
Afyon Lojistik
Ünye Yol Yardım
Kırşehir Parça Eşya Taşıma
Düzce Şehirler Arası Nakliyat
Uşak Şehirler Arası Nakliyat
Kırklareli Lojistik
CA3E0
ReplyDeleteMalatya Evden Eve Nakliyat
İstanbul Evden Eve Nakliyat
Çankırı Evden Eve Nakliyat
Bolu Evden Eve Nakliyat
İzmir Evden Eve Nakliyat
Çerkezköy Koltuk Kaplama
Poloniex Güvenilir mi
Çerkezköy Oto Boya
Ağrı Evden Eve Nakliyat
71A20
ReplyDeleteKilis Evden Eve Nakliyat
Denizli Evden Eve Nakliyat
Coin Nedir
Ünye Yol Yardım
Ankara Boya Ustası
Binance Referans Kodu
Kırıkkale Evden Eve Nakliyat
Poloniex Güvenilir mi
Ünye Oto Elektrik
08A85
ReplyDeletebinance
EE227
ReplyDeletebinance referans kodu
resimli magnet
resimli magnet
referans kimliği nedir
binance referans kodu
binance referans kodu
referans kimliği nedir
resimli magnet
binance referans kodu
15067
ReplyDeletemobil proxy 4g
binance referans kimliği
mexc
bitexen
bybit
telegram türk kripto kanalları
bitget
okex
türk kripto telegram grupları
YHMJUYKHI
ReplyDeleteشركة كشف تسربات المياه بالقطيف
شركة تنظيف بخميس مشيط A46HA79LJV
ReplyDeleteThanks and that i have a neat present: Whole House Remodel Cost house renovation financing
ReplyDelete<a href="https://zahretelmamlakah.com/%d8%b4%d8%b1%d9%83%d8%a9-%d8%aa%d9%86%d8%b8%d9%8a%d9%81-%d8%b3%d8%ac%d8%a7%d
ReplyDeleteشركة صيانة افران hon4vqTczQ
ReplyDeleteشركة عزل اسطح بالجبيل Rq67SQrYqH
ReplyDelete