Click here to Skip to main content
16,017,788 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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";
}


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.
Posted
Updated 19-May-19 5:43am
v2
Comments
Richard MacCutchan 19-May-19 3:19am    
What is the problem?
Member 14367500 19-May-19 4:52am    
i am not able to download MS-Excel file via my compiler

1 solution

Considering your trivial data requirements, just create a CSV file and open it in Excel.

Once your requirements evolve, you can consider more "complicated" options.

1, "Amit", "Shukla"
2, "Lokesh", "Gupta"
3, "John", "Adwards"
4, "Brian", "Schultz"
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900