Copy the excel sheet and write data to the excel sheet

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

 

//copy the excel file

public void copy(File sourceFile, File targetFile) {
if (targetFile.exists()) {
targetFile.delete();
}
int len = (int) sourceFile.length();
byte[] data = new byte[len];
FileInputStream input = null;
FileOutputStream output = null;
try {
input = new FileInputStream(sourceFile);
output = new FileOutputStream(targetFile);
input.read(data);
output.write(data);
} catch (Exception ex) {
log.error("Cannot init student template {}", targetFile, ex);
} finally {
if (input != null) {
try {
input.close();
} catch (IOException e) {
}
}
if (output != null) {
try {
output.close();
} catch (IOException e) {
}
}
}
}

 

//Write data to excel sheet

public void writeExcel(List<Map> templateList, int m, String path) {
OutputStream out = null;
try {
// Get the total number of columns
int columnNumCount = m;
// Read the Excel document
File finalXlsxFile = new File(path);
Workbook workBook = getWorkbok(finalXlsxFile);
// sheet corresponds to a work page
Sheet sheet = workBook.getSheetAt(0);
Sheet sheet1 = workBook.getSheetAt(1);
/**
* Delete the original data, except for the attribute column
*/
int rowNumber = sheet.getLastRowNum(); // The first row starts from 0
System.out.println("The total number of rows in the original data, except the attribute column: " + rowNumber);
for (int i = 1; i <= rowNumber; i++) {
Row row = sheet.getRow(i);
sheet.removeRow(row);
}
/**
* Write new data to Excel
*/
for (int j = 0; j < templateList.size(); j++) {
// create a row: start from the second row, skip the attribute column
Row row = sheet.createRow(j + 1);
Row row1 = null;
// Get each record to be inserted
Map dataMap = templateList.get(j);
String name = dataMap.get("name").toString();
String subjectName = null;
if(dataMap.containsKey("subjectName") ){
subjectName = dataMap.get("subjectName").toString();
row1 = sheet1.createRow(j + 1);
}
for (int k = 0; k <= columnNumCount; k++) {
// loop within a row (column)
Cell first = row.createCell(0);
first.setCellValue(name);
if(subjectName != null){
Cell secend = row1.createCell(0);
secend.setCellValue(subjectName);
}
}
}
// Create a file output stream, ready to output the spreadsheet: this is a must, otherwise anything you do on the sheet won't work
out = new FileOutputStream(finalXlsxFile.getAbsolutePath());
workBook.write(out);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (out != null) {
out.flush();
out.close();
}
} catch (IOException e) {
e.printStackTrace() ;
}
}
System.out.println("Data exported successfully");
}

 

/**
* 判断Excel的版本,获取Workbook
*
* @param in
* @param filename
* @return
* @throws IOException
*/
public static Workbook getWorkbok(File file) throws IOException {
Workbook wb = null;
FileInputStream in = new FileInputStream(file);
if (file.getName().endsWith(EXCEL_XLS)) { // Excel 2003
wb = new HSSFWorkbook(in);
} else if (file.getName().endsWith(EXCEL_XLSX)) { // Excel 2007/2010
wb = new XSSFWorkbook(in);
}
return wb;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324619783&siteId=291194637