Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use POI to export data to Excel file in Java project

2025-03-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains "how to use POI to export data to Excel file in Java project". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to export data to Excel file using POI in Java project".

I. Preface

Jar package poi-3.17.jar that needs to be used

Second, the specific implementation step / / the first step is to create a webbook corresponding to an Excel file HSSFWorkbook wb=new HSSFWorkbook (); / / the second step is to add a sheet to the webbook corresponding to the sheet HSSFSheet sheet=wb.createSheet in the Excel file ("food information data"); / / the third step is to add the header line 0 HSSFRow row = sheet.createRow (0) to the sheet / / step 4, create the cell and center the header HSSFCellStyle style = wb.createCellStyle (); style.setAlignment (HorizontalAlignment.CENTER); / / centered format HSSFCell cell=row.createCell (0); cell.setCellValue ("number"); cell.setCellStyle (style); cell=row.createCell ((short) 1); cell.setCellValue ("name") Cell.setCellStyle (style); cell=row.createCell ((short) 2); cell.setCellValue ("Type"); cell.setCellStyle (style); cell=row.createCell ((short) 3); cell.setCellValue ("Unit Price"); cell.setCellStyle (style); cell=row.createCell ((short) 4); cell.setCellValue ("inventory") Cell.setCellStyle (style); / / the fifth step is to write the physical data and get the data from the database FoodController controller=new FoodController (); List foodsList = controller.foodsList (null, null); for (int I = 0; I < foodsList.size (); iTunes +) {/ / create the cell and assign the value row=sheet.createRow (iTun1) Foods foods = foodsList.get (I); row.createCell ((short) 0) .setCellValue (foods.getId ()); row.createCell ((short) 1) .setCellValue (foods.getName ()); row.createCell ((short) 2) .setCellValue (foods.getType ()); row.createCell ((short) 3) .setCellValue (foods.getPrice ()) Row.createCell ((short) 4) .setCellValue (foods.getNum ());} / / step 6, download Excel OutputStream out=null; out=response.getOutputStream (); String fileName= "Food Information .xls"; response.setContentType ("application/x-=msdownload"); response.setHeader ("Content-Disposition", "attachment" Filename= "+ URLEncoder.encode (fileName," UTF-8 ")); wb.write (out) Thank you for your reading, the above is the content of "how to export data to Excel file with POI in Java project". After the study of this article, I believe you have a deeper understanding of how to export data to Excel file with POI in Java project, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report