導(dǎo)出時(shí)自由選擇路徑的代碼如下:
創(chuàng)新互聯(lián)建站-專業(yè)網(wǎng)站定制、快速模板網(wǎng)站建設(shè)、高性價(jià)比柳林網(wǎng)站開發(fā)、企業(yè)建站全套包干低至880元,成熟完善的模板庫,直接使用。一站式柳林網(wǎng)站制作公司更省心,省錢,快速模板網(wǎng)站建設(shè)找我們,業(yè)務(wù)覆蓋柳林地區(qū)。費(fèi)用合理售后完善,十年實(shí)體公司更值得信賴。
1、后臺輸出Excel文件代碼:
OutputStream output = response.getOutputStream();
response.reset();
response.setHeader("Content-disposition", "attachment; filename=" + path);
response.setContentType("Content-Type:application/vnd.ms-excel ");
wb.write(output);
output.close();
2、前端代碼:
window.open("getExcelList","_blank");
導(dǎo)出excel數(shù)據(jù)
* @param id
* @param m
* @return
*/
@RequestMapping("/exportExcel")
public void exportExcel(@RequestParam("id") Integer id, Model m,HttpServletRequest req, HttpServletResponse resp) {
try {
ExportExcelP2pLoanPlanVo ex = new ExportExcelP2pLoanPlanVo();
String[] headers = {"最遲還款日", "還款金額","剩余幾天","逾期幾天", "罰息","是否墊付","狀態(tài)","是否發(fā)放收益"};
ListP2pLoanPlanVo dataset = new ArrayListP2pLoanPlanVo();
ListP2pLoanPlan plans = this.planService.getListByLoan(id);
for (int i = 0; i plans.size(); i++) {
P2pLoanPlanVo p2pLoanPlanVo = new P2pLoanPlanVo();
p2pLoanPlanVo.setRepayDate(plans.get(i).getRepayDate());
p2pLoanPlanVo.setRepayAmount(plans.get(i).getRepayAmount());
if(plans.get(i).getRepayDays() = 0 plans.get(i).getStatus() == 0){
p2pLoanPlanVo.setRepayDays(plans.get(i).getRepayDays());
}else{
p2pLoanPlanVo.setRepayDays(0);
}
if(plans.get(i).getRepayDays() 0 plans.get(i).getStatus() == 0){
p2pLoanPlanVo.setRepayYqDays(-plans.get(i).getRepayDays());
}else{
p2pLoanPlanVo.setRepayYqDays(0);
}
package com.test;
/**
* 需要的jar包:
* poi-3.0.2-FINAL-20080204.jar
* poi-contrib-3.0.2-FINAL-20080204.jar
*poi-scratchpad-3.0.2-FINAL-20080204.jar
* poi-3.5-beta6-20090622.jar
* geronimo-stax-api_1.0_spec-1.0.jar
* ooxml-schemas-1.0.jar
* openxml4j-bin-beta.jar
* poi-ooxml-3.5-beta6-20090622.jar
*xmlbeans-2.3.0.jar
* dom4j-1.6.1.jar
*/
import java.io.ByteArrayInputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringWriter;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.util.PDFTextStripper;
import org.apache.poi.POIOLE2TextExtractor;
import org.apache.poi.POITextExtractor;
import org.apache.poi.POIXMLDocument;
import org.apache.poi.POIXMLTextExtractor;
import org.apache.poi.extractor.ExtractorFactory;
import org.apache.poi.hssf.usermodel.HSSFCell;
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.hwpf.extractor.WordExtractor;
import org.apache.poi.openxml4j.exceptions.OpenXML4JException;
import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.poifs.filesystem.DirectoryEntry;
import org.apache.poi.poifs.filesystem.DocumentEntry;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.xslf.extractor.XSLFPowerPointExtractor;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.xwpf.extractor.XWPFWordExtractor;
import org.apache.xmlbeans.XmlException;
public class WordAndExcelExtractor {
public static void main(String[] args) {
try {
// 讀取word
String wordFile = "D:/1.doc";
//String wordText2007 = WordAndExcelExtractor.extractTextFromDOC2007(wordFile);
//System.out.println("wordText2007=======" + wordText2007);
InputStream isword = new FileInputStream(wordFile);
WordExtractor wordExtractor = new WordExtractor(isword);
System.out.println("word========" + wordExtractor.getText());
// 讀取 Excel
InputStream is = new FileInputStream("D:/測試.xls");
String excelText = WordAndExcelExtractor.extractTextFromXLS(is);
System.out.println("text2003==========" + excelText);
String excelFile = "D:/test2.xlsx";
String excelText2007 = WordAndExcelExtractor
.extractTextFromXLS2007(excelFile);
System.out.println("excelText2007==========" + excelText2007);
// 讀取 PPT
PowerPointExtractor ppe = new PowerPointExtractor("D:/test.ppt");
System.out.println("ppt2003===============" + ppe.getText());
// System.out.println("###############################");
// System.out.println(ppe.getText(true, true, true, true));
//
// InputStream is = new FileInputStream("D:/test.ppt");
// PowerPointExtractor ppt2003 = new PowerPointExtractor(is);
// System.out.println(ppt2003.getText());
System.out.println("************************************");
XSLFPowerPointExtractor ppt = new XSLFPowerPointExtractor(
POIXMLDocument.openPackage("D:/test2.pptx"));
System.out.println("ppt2007============================="
+ ppt.getText());
/* 讀取PDF */
InputStream in = new FileInputStream("D:/test.pdf");
PDDocument pdfDocument = PDDocument.load(in);
if (pdfDocument.isEncrypted()) {
// 僅僅嘗試使用默認(rèn)密碼打開加密的PDF
pdfDocument.decrypt("");
}
PDFTextStripper stripper = null;
// 創(chuàng)建一個(gè)writer用來作來存儲文件正文
StringWriter writer = new StringWriter();
if (stripper == null) {
stripper = new PDFTextStripper();
} else {
stripper.resetEngine();
}
stripper.writeText(pdfDocument, writer);
String contents = writer.getBuffer().toString();
System.out.println("pdfd===" + contents);
/* 向Word中寫入數(shù)據(jù) */
byte[] a = contents.getBytes();
ByteArrayInputStream bs = new ByteArrayInputStream(a);
POIFSFileSystem fs = new POIFSFileSystem();
// /////////////////////////////////
DirectoryEntry directory = fs.getRoot();
DocumentEntry de = directory.createDocument("WordDocument", bs);
// 以上兩句代碼不能省略,否則輸出的是亂碼
FileOutputStream fos = new FileOutputStream("D:\\dd.doc");
fs.writeFilesystem(fos);
bs.close();
fos.flush();
fos.close();
System.out.println("寫入成功");
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* @Method: extractTextFromXLS
* @Description: 從excel 2003檔中提取純文本
*
* @param
* @return String
* @throws
*/
@SuppressWarnings("deprecation")
private static String extractTextFromXLS(InputStream is) throws IOException {
StringBuffer content = new StringBuffer();
HSSFWorkbook workbook = new HSSFWorkbook(is); // 創(chuàng)建對Excel工作簿文件的引用
for (int numSheets = 0; numSheets workbook.getNumberOfSheets(); numSheets++) {
if (null != workbook.getSheetAt(numSheets)) {
HSSFSheet aSheet = workbook.getSheetAt(numSheets); // 獲得一個(gè)sheet
content.append(aSheet.getSheetName());
content.append("\r\n-----------------------\r\n");
for (int rowNumOfSheet = 0; rowNumOfSheet = aSheet
.getLastRowNum(); rowNumOfSheet++) {
if (null != aSheet.getRow(rowNumOfSheet)) {
HSSFRow aRow = aSheet.getRow(rowNumOfSheet); // 獲得一行
for (short cellNumOfRow = 0; cellNumOfRow = aRow
.getLastCellNum(); cellNumOfRow++) {
if (null != aRow.getCell(cellNumOfRow)) {
HSSFCell aCell = aRow.getCell(cellNumOfRow); // 獲得列值
if (aCell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC) {
content.append(aCell.getNumericCellValue());
} else if (aCell.getCellType() == HSSFCell.CELL_TYPE_BOOLEAN) {
content.append(aCell.getBooleanCellValue());
} else {
content.append(aCell.getStringCellValue());
}
content.append("\t");
}
}
content.append("\r\n");
}
}
}
}
return content.toString();
}
/**
* @Method: extractTextFromXLS2007
* @Description: 從excel 2007文檔中提取純文本
*
* @param
* @return String
* @throws
*/
private static String extractTextFromXLS2007(String fileName)
throws Exception {
StringBuffer content = new StringBuffer();
// 構(gòu)造 XSSFWorkbook 對象,strPath 傳入文件路徑
XSSFWorkbook xwb = new XSSFWorkbook(fileName);
// 循環(huán)工作表Sheet
for (int numSheet = 0; numSheet xwb.getNumberOfSheets(); numSheet++) {
XSSFSheet xSheet = xwb.getSheetAt(numSheet);
if (xSheet == null) {
continue;
}
// 循環(huán)行Row
for (int rowNum = 0; rowNum = xSheet.getLastRowNum(); rowNum++) {
XSSFRow xRow = xSheet.getRow(rowNum);
if (xRow == null) {
continue;
}
// 循環(huán)列Cell
for (int cellNum = 0; cellNum = xRow.getLastCellNum(); cellNum++) {
XSSFCell xCell = xRow.getCell(cellNum);
if (xCell == null) {
continue;
}
if (xCell.getCellType() == XSSFCell.CELL_TYPE_BOOLEAN) {
content.append(xCell.getBooleanCellValue());
} else if (xCell.getCellType() == XSSFCell.CELL_TYPE_NUMERIC) {
content.append(xCell.getNumericCellValue());
} else {
content.append(xCell.getStringCellValue());
}
}
}
}
return content.toString();
}
}
這是POI jar包的下載地址,我下載的是3.9版本的
POI導(dǎo)出自動求和
公共POI導(dǎo)出Excel方法--java
阿里的EasyExcel,性能更好的導(dǎo)出Excel第三方工具
我建議你這樣做,
準(zhǔn)備一份excel做模板
把需要導(dǎo)出的數(shù)據(jù)分為兩類:1.excel每列的名稱ListString2.excel每行的數(shù)據(jù)ListObject[],其中Object[]對應(yīng)一行數(shù)據(jù)
到時(shí)候程序只需要把需要導(dǎo)出的數(shù)據(jù)轉(zhuǎn)換為第2點(diǎn)所說的格式,然后你用poi按行寫入模板e(cuò)xcel,然后再提供給客戶就可以了。
這個(gè)方法適用于比較簡單的情況,所有數(shù)據(jù)也是寫在一個(gè)sheet中的,如果你要分sheet,建議特殊情況特殊處理,單獨(dú)出來做。
如果還要問詳細(xì)的問題,我可以幫你,建議你留下QQ,我加你
我白天不一定有空哦。晚上應(yīng)該有時(shí)間。
項(xiàng)目結(jié)構(gòu):
xls:
\\\
XlsMain .java 類
//該類有main方法,主要負(fù)責(zé)運(yùn)行程序,同時(shí)該類中也包含了用poi讀取Excel(2003版)
*
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
/**
*
* @author Hongten/br
*
* 參考地址:
*
*/
public class XlsMain {
public static void main(String[] args) throws IOException {
XlsMain xlsMain = new XlsMain();
XlsDto xls = null;
ListXlsDto list = xlsMain.readXls();
try {
XlsDto2Excel.xlsDto2Excel(list);
} catch (Exception e) {
e.printStackTrace();
}
for (int i = 0; i list.size(); i++) {
xls = (XlsDto) list.get(i);
System.out.println(xls.getXh() + " " + xls.getXm() + " "
+ xls.getYxsmc() + " " + xls.getKcm() + " "
+ xls.getCj());
}
}
/**
* 讀取xls文件內(nèi)容
*
* @return ListXlsDto對象
* @throws IOException
* 輸入/輸出(i/o)異常
*/
private ListXlsDto readXls() throws IOException {
InputStream is = new FileInputStream("pldrxkxxmb.xls");
HSSFWorkbook hssfWorkbook = new HSSFWorkbook(is);
XlsDto xlsDto = null;
ListXlsDto list = new ArrayListXlsDto();
// 循環(huán)工作表Sheet
for (int numSheet = 0; numSheet hssfWorkbook.getNumberOfSheets(); numSheet++) {
HSSFSheet hssfSheet = hssfWorkbook.getSheetAt(numSheet);
if (hssfSheet == null) {
continue;
}
// 循環(huán)行Row
for (int rowNum = 1; rowNum = hssfSheet.getLastRowNum(); rowNum++) {
HSSFRow hssfRow = hssfSheet.getRow(rowNum);
if (hssfRow == null) {
continue;
}
xlsDto = new XlsDto();
// 循環(huán)列Cell
// 0學(xué)號 1姓名 2學(xué)院 3課程名 4 成績
// for (int cellNum = 0; cellNum =4; cellNum++) {
HSSFCell xh = hssfRow.getCell(0);
if (xh == null) {
continue;
}
xlsDto.setXh(getValue(xh));
HSSFCell xm = hssfRow.getCell(1);
if (xm == null) {
continue;
}
xlsDto.setXm(getValue(xm));
HSSFCell yxsmc = hssfRow.getCell(2);
if (yxsmc == null) {
continue;
}
xlsDto.setYxsmc(getValue(yxsmc));
HSSFCell kcm = hssfRow.getCell(3);
if (kcm == null) {
continue;
}
xlsDto.setKcm(getValue(kcm));
HSSFCell cj = hssfRow.getCell(4);
if (cj == null) {
continue;
}
xlsDto.setCj(Float.parseFloat(getValue(cj)));
list.add(xlsDto);
}
}
return list;
}
/**
* 得到Excel表中的值
*
* @param hssfCell
* Excel中的每一個(gè)格子
* @return Excel中每一個(gè)格子中的值
*/
@SuppressWarnings("static-access")
private String getValue(HSSFCell hssfCell) {
if (hssfCell.getCellType() == hssfCell.CELL_TYPE_BOOLEAN) {
// 返回布爾類型的值
return String.valueOf(hssfCell.getBooleanCellValue());
} else if (hssfCell.getCellType() == hssfCell.CELL_TYPE_NUMERIC) {
// 返回?cái)?shù)值類型的值
return String.valueOf(hssfCell.getNumericCellValue());
} else {
// 返回字符串類型的值
return String.valueOf(hssfCell.getStringCellValue());
}
}
}
XlsDto2Excel.java類
//該類主要負(fù)責(zé)向Excel(2003版)中插入數(shù)據(jù)
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.List;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
public class XlsDto2Excel {
/**
*
* @param xls
* XlsDto實(shí)體類的一個(gè)對象
* @throws Exception
* 在導(dǎo)入Excel的過程中拋出異常
*/
public static void xlsDto2Excel(ListXlsDto xls) throws Exception {
// 獲取總列數(shù)
int CountColumnNum = xls.size();
// 創(chuàng)建Excel文檔
HSSFWorkbook hwb = new HSSFWorkbook();
XlsDto xlsDto = null;
// sheet 對應(yīng)一個(gè)工作頁
HSSFSheet sheet = hwb.createSheet("pldrxkxxmb");
HSSFRow firstrow = sheet.createRow(0); // 下標(biāo)為0的行開始
HSSFCell[] firstcell = new HSSFCell[CountColumnNum];
String[] names = new String[CountColumnNum];
names[0] = "學(xué)號";
names[1] = "姓名";
names[2] = "學(xué)院";
names[3] = "課程名";
names[4] = "成績";
for (int j = 0; j CountColumnNum; j++) {
firstcell[j] = firstrow.createCell(j);
firstcell[j].setCellValue(new HSSFRichTextString(names[j]));
}
for (int i = 0; i xls.size(); i++) {
// 創(chuàng)建一行
HSSFRow row = sheet.createRow(i + 1);
// 得到要插入的每一條記錄
xlsDto = xls.get(i);
for (int colu = 0; colu = 4; colu++) {
// 在一行內(nèi)循環(huán)
HSSFCell xh = row.createCell(0);
xh.setCellValue(xlsDto.getXh());
HSSFCell xm = row.createCell(1);
xm.setCellValue(xlsDto.getXm());
HSSFCell yxsmc = row.createCell(2);
yxsmc.setCellValue(xlsDto.getYxsmc());
HSSFCell kcm = row.createCell(3);
kcm.setCellValue(xlsDto.getKcm());
HSSFCell cj = row.createCell(4);
cj.setCellValue(xlsDto.getCj());
(xlsDto.getMessage());
}
}
// 創(chuàng)建文件輸出流,準(zhǔn)備輸出電子表格
OutputStream out = new FileOutputStream("POI2Excel/pldrxkxxmb.xls");
hwb.write(out);
out.close();
System.out.println("數(shù)據(jù)庫導(dǎo)出成功");
}
}
XlsDto .java類
//該類是一個(gè)實(shí)體類
public class XlsDto {
/**
* 選課號
*/
private Integer xkh;
/**
* 學(xué)號
*/
private String xh;
/**
* 姓名
*/
private String xm;
/**
* 學(xué)院
*/
private String yxsmc;
/**
* 課程號
*/
private Integer kch;
/**
* 課程名
*/
private String kcm;
/**
* 成績
*/
private float cj;
public Integer getXkh() {
return xkh;
}
public void setXkh(Integer xkh) {
this.xkh = xkh;
}
public String getXh() {
return xh;
}
public void setXh(String xh) {
this.xh = xh;
}
public String getXm() {
return xm;
}
public void setXm(String xm) {
this.xm = xm;
}
public String getYxsmc() {
return yxsmc;
}
public void setYxsmc(String yxsmc) {
this.yxsmc = yxsmc;
}
public Integer getKch() {
return kch;
}
public void setKch(Integer kch) {
this.kch = kch;
}
public String getKcm() {
return kcm;
}
public void setKcm(String kcm) {
this.kcm = kcm;
}
public float getCj() {
return cj;
}
public void setCj(float cj) {
this.cj = cj;
}
}
后臺輸出:
數(shù)據(jù)庫導(dǎo)出成功
1.0 hongten 信息技術(shù)學(xué)院 計(jì)算機(jī)網(wǎng)絡(luò)應(yīng)用基礎(chǔ) 80.0
2.0 王五 信息技術(shù)學(xué)院 計(jì)算機(jī)網(wǎng)絡(luò)應(yīng)用基礎(chǔ) 81.0
3.0 李勝基 信息技術(shù)學(xué)院 計(jì)算機(jī)網(wǎng)絡(luò)應(yīng)用基礎(chǔ) 82.0
4.0 五班古 信息技術(shù)學(xué)院 計(jì)算機(jī)網(wǎng)絡(luò)應(yīng)用基礎(chǔ) 83.0
5.0 蔡詩蕓 信息技術(shù)學(xué)院 計(jì)算機(jī)網(wǎng)絡(luò)應(yīng)用基礎(chǔ) 84.0
String?temFilePath?=?getServletContext().getRealPath("/")?+?"uploadfiles/"?+?tmpXlsName;
File?file?=?new?File(temFilePath);
InputStream?input?=new?FileInputStream(file);
Workbook?wb?=?WorkbookFactory.create(input);?//?根據(jù)模板生成新的excel文件
input.close();
Sheet?sheet?=?wb.getSheetAt(0);
while?(rs.next())?{
Row?row?=sheet.getRow(++rowIdx);
if(row==null){
row?=sheet.createRow(rowIdx);
}
for?(int?cIdx=Integer.parseInt(colStartIdx)-1,colIdx=1;?cIdxInteger.parseInt(colEndIdx);?cIdx++,colIdx++)?{
Object?value?=?rs.getObject(colIdx);
Cell?cell?=?row.createCell(cIdx);
cell.setCellValue(value==null?"":value.toString());
cell.setCellStyle(cs);
}
}大概代碼就是這樣,根據(jù)你的模板,生成文件后,。。。