Import data into Excel and download function

1, create an ExcelExportAction class

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor;
import com.opensymphony.xwork2.ActionSupport;
public class ExcelExportAction extends ActionSupport{
    private static final long serialVersionUID = 4565154051377016557L;
    // private static final long cen = 2565374152L;
    Private static final String[] header = { "serial number", "employee number", "employee name", "affiliation department", "position", "job date", "remark" };
    private String excelFileName;
    private InputStream excelStream;

    /**
     * export excel
     * 
     * @return
     */
    public String exportExcel() {
	try {
	     // Create excel
	    HSSFWorkbook workbook = new HSSFWorkbook();
	     HSSFSheet sheet = workbook.createSheet("test file 1");
	     // Here is createRow(int), don't remember with sheet.getRow(int). . . .
	    HSSFRow row = sheet.createRow(0);
	    row.setHeightInPoints(25);
	    HSSFCellStyle cellstyle = workbook.createCellStyle();
	    cellstyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
	    cellstyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
	    cellstyle.setFillForegroundColor(HSSFColor.GREY_25_PERCENT.index);
	    cellstyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
	    HSSFFont font = workbook.createFont();
	    font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
	    cellstyle.setFont(font);
	    HSSFCell cell;
	    for (int i = 0; i < header.length; i++) {
		cell = row.createCell(i);
		cell.setCellStyle(cellstyle);
		sheet.setColumnWidth(i,25*256);
		cell.setCellValue(header[i]);
	    }
	     // Define 20 rows of data to be inserted into the excel table
	    for (int j = 1; j <= 50; j++) {
		row = sheet.createRow(j);
		row.setHeightInPoints(20);
		HSSFCell cell0 = row.createCell(0);
		cell0.setCellValue(j);

		HSSFCell cell1 = row.createCell(1);
		 cell1.setCellValue("employee number" + j);

		HSSFCell cell2 = row.createCell(2);
		 cell2.setCellValue("employee name" + j);

		HSSFCell cell3 = row.createCell(3);
		 cell3.setCellValue("Soft Research" + j + "Parts");

		HSSFCell cell4 = row.createCell(4);
		 cell4.setCellValue("programmer" + j);

		HSSFCell cell5 = row.createCell(5);
		cell5.setCellValue("2013-05-28");

		HSSFCell cell6 = row.createCell(6);
		cell6.setCellValue("remark");
	    }
	     // save the file in the stream
	    ByteArrayOutputStream bos = new ByteArrayOutputStream();
	    workbook.write(bos);
	    byte[] fileContent = bos.toByteArray();
	    ByteArrayInputStream bis = new ByteArrayInputStream(fileContent);

	    excelStream = bis;
	     excelFileName = "EXCEL file export test.xls";
	    excelFileName = new String(excelFileName.getBytes(),"iso-8859-1");
	} catch (IOException e) {
	    e.printStackTrace();
	}

	return SUCCESS;
    }

    /** getter/setter method **/
    public String getExcelFileName() {
	return excelFileName;
    }

    public void setExcelFileName(String excelFileName) {
	this.excelFileName = excelFileName;
    }

    public InputStream getExcelStream() {
	return excelStream;
    }

    public void setExcelStream(InputStream excelStream) {
	this.excelStream = excelStream;
    }

}

2, struts.xml configuration

<!-- exprot excel file -->
		<action name="exportExcel" class="com.hnyyzw.trm.action.ExcelExportAction"
			method="exportExcel">
			<result name="success" type="stream">
				 <!-- Download file type -->
				<param name="contentType">application/vnd.ms-excel</param>
				 <!-- return stream excelStream variable name in Action -->
				<param name="inputName">excelStream</param>
				 <!-- attachment The parameters of this location are quite special. You can set whether to download a prompt box when downloading, or download it directly. fileName specifies the name of the generated file (suitable for dynamically generating file names, such as when reporting, generally speaking, it is a few months of statistical data) for the action variable -->
				<param name="contentDisposition">attachment;filename=${excelFileName}</param>
				<param name="bufferSize">1024</param>
			</result>
		</action>

 

Intelligent Recommendation

Excel function usage (batch data import into MySql)

Application scenario one Excel table, many data need to be imported into MySql database table First write the required SQL statement, example ↓: Excel table data Excel replaces all with one click...

EAS custom import Excel data function

Customize the function of importing Excel data...

Precautions for Excel batch import initialization data function

A recent party building project (a housing management bureau, in fact, the project is divided into three sub-systems: party building, personnel, and OA. The party building and personnel system has bee...

Transformation of original Excel import data function

Clean the original Excel import function The original Excel import function is mainly used to implement the generalized Excel import function for various modules of SaaS system. The interface is unifi...

More Recommendation

Excel Import - Export Download

Excel Import Export Features Reference article: Java backend Excel data import Pom.xml dependence Java code...

Use the SQL Server data import function to import the number of Excel

At the beginning of the system, basic data and historical data will be used more often. The function of importing data is provided in SQL Server, you can import the Excel data provided by the user to ...

EXCEL data download function in the front end

I often encounter the need to download the EXCEL table data in the work. The back -end is generally returned to the format of file streams. The front end needs to be parsed into the local form of Exce...

java download excel import excel read excel data, insert the data into the database

10: If you want to export excel sheet, please link: The imported effect is as follows: 1: Start page 2: Click the download Excel template button to achieve the effect: 3: Select the save file and clic...

php import data to excel and download (this article is purely for study notes, not as a tutorial)

php import data to excel and download First send the data to the back-end processing in the html interface, getOrderExport is the method name to pass the data in the past Need to download PHPExcel cla...

Copyright  DMCA © 2018-2026 - All Rights Reserved - www.programmersought.com  User Notice

Top