本文介绍了我想使用APACHE POI LIBRARY从我的java SPRING MVC控制器创建一个可下载的excel文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Please I need A source Code

import java.io.ByteArrayOutputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Set;
import java.util.Calendar;
import java.util.Date;
import java.util.*;
import java.util.TreeMap;
import java.io.File;
import java.io.IOException;
import java.io.FileInputStream;
import java.util.Iterator;
import java.util.Scanner;
import java.io.FileOutputStream;  
import java.util.Date;
import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.nio.file.Files;
import java.nio.file.Paths;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.CellStyle;
//import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFColor;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.web.servlet.ModelAndView;

@RequestMapping(value="/excel", method=RequestMethod.GET)
 // public ResponseEntity<byte[]> poi(Model model, HttpServletRequest httpServletRequest, HttpServletResponse response) {
public  String poi(){
XSSFWorkbook workbook = new XSSFWorkbook();
XSSFSheet sheet = workbook.createSheet("Employee Data");
		



Map<String, Object[]> data = new TreeMap<String, Object[]>();
data.put("1", new Object[] {"ID", "NAME", "LASTNAME"});
data.put("2", new Object[] {1, "Amit", "Shukla"});
data.put("3", new Object[] {2, "Lokesh", "Gupta"});
data.put("4", new Object[] {3, "John", "Adwards"});
data.put("5", new Object[] {4, "Brian", "Schultz"});
		
Set<String> keyset = data.keySet();
int rownum = 0;
for (String key : keyset)
{
XSSFRow row = sheet.createRow(rownum++);
Object [] objArr = data.get(key);
int cellnum = 0;
for (Object obj : objArr)
{
XSSFCell cell = row.createCell(cellnum++);
if(obj instanceof String)
cell.setCellValue((String)obj);
else if(obj instanceof Integer)
cell.setCellValue((Integer)obj);
}
}	

try
{
FileOutputStream out = new FileOutputStream(new File("excel_demo.xlsx"));
workbook.write(out);
out.close();
System.out.println("excel_demo.xlsx write succesfuly ondisk.");			
}catch (Exception e) 
{e.printStackTrace();
}
HttpHeaders headers = new HttpHeaders();
headers.add("Content-Disposition", "attachment; filename=excel-demo.xlsx");

 return ResponseEntity
 .ok()
 .headers(headers)
 .contentType(MediaType.APPLICATION_ALL)
 .body(outStream.toByteArray());
 return "candidate/excel";
}





我的尝试:



上面的代码是我试过的,在我的项目中使用apache POI库。

这是创建一个Excel文件并将其返回以便浏览器下载。



What I have tried:

the aboved code is what i hv tried, using apache POI Library in my project.
this is to create an Excel file and return it so that it is downloaded by the browser.

推荐答案


这篇关于我想使用APACHE POI LIBRARY从我的java SPRING MVC控制器创建一个可下载的excel文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-02 00:17