溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶(hù)服務(wù)條款》

使用java怎么導(dǎo)出Excel

發(fā)布時(shí)間:2021-05-19 16:01:58 來(lái)源:億速云 閱讀:125 作者:Leah 欄目:編程語(yǔ)言

這期內(nèi)容當(dāng)中小編將會(huì)給大家?guī)?lái)有關(guān)使用java怎么導(dǎo)出Excel,文章內(nèi)容豐富且以專(zhuān)業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。

首先需要引入的jar包:

使用java怎么導(dǎo)出Excel

然后就是正式代碼了。

package lcy._41_50;
 
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.net.URLEncoder;
 
import javax.servlet.http.HttpServletResponse;
 
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.CellRangeAddress;
import org.apache.poi.hssf.util.HSSFColor;
 
@SuppressWarnings( { "deprecation" })
public class Test46 {
 
	public static void main(String[] args) throws Exception {
 
		String sheetName = "用車(chē)統(tǒng)計(jì)表單";
		String titleName = "用車(chē)申請(qǐng)數(shù)據(jù)統(tǒng)計(jì)表";
		String fileName = "用車(chē)申請(qǐng)統(tǒng)計(jì)表單";
		int columnNumber = 3;
		int[] columnWidth = { 10, 20, 30 };
		String[][] dataList = { { "001", "2015-01-01", "IT" },
				{ "002", "2015-01-02", "市場(chǎng)部" }, { "003", "2015-01-03", "測(cè)試" } };
		String[] columnName = { "單號(hào)", "申請(qǐng)時(shí)間", "申請(qǐng)部門(mén)" };
		new Test46().ExportNoResponse(sheetName, titleName, fileName,
				columnNumber, columnWidth, columnName, dataList);
	}
 
	public void ExportWithResponse(String sheetName, String titleName,
			String fileName, int columnNumber, int[] columnWidth,
			String[] columnName, String[][] dataList,
			HttpServletResponse response) throws Exception {
		if (columnNumber == columnWidth.length&& columnWidth.length == columnName.length) {
			// 第一步,創(chuàng)建一個(gè)webbook,對(duì)應(yīng)一個(gè)Excel文件
			HSSFWorkbook wb = new HSSFWorkbook();
			// 第二步,在webbook中添加一個(gè)sheet,對(duì)應(yīng)Excel文件中的sheet
			HSSFSheet sheet = wb.createSheet(sheetName);
			// sheet.setDefaultColumnWidth(15); //統(tǒng)一設(shè)置列寬
			for (int i = 0; i < columnNumber; i++) 
			{
				for (int j = 0; j <= i; j++) 
				{
					if (i == j) 
					{
						sheet.setColumnWidth(i, columnWidth[j] * 256); // 單獨(dú)設(shè)置每列的寬
					}
				}
			}
			// 創(chuàng)建第0行 也就是標(biāo)題
			HSSFRow row1 = sheet.createRow((int) 0);
			row1.setHeightInPoints(50);// 設(shè)備標(biāo)題的高度
			// 第三步創(chuàng)建標(biāo)題的單元格樣式style2以及字體樣式headerFont1
			HSSFCellStyle style2 = wb.createCellStyle();
			style2.setAlignment(HSSFCellStyle.ALIGN_CENTER);
			style2.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
			style2.setFillForegroundColor(HSSFColor.LIGHT_TURQUOISE.index);
			style2.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
			HSSFFont headerFont1 = (HSSFFont) wb.createFont(); // 創(chuàng)建字體樣式
			headerFont1.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); // 字體加粗
			headerFont1.setFontName("黑體"); // 設(shè)置字體類(lèi)型
			headerFont1.setFontHeightInPoints((short) 15); // 設(shè)置字體大小
			style2.setFont(headerFont1); // 為標(biāo)題樣式設(shè)置字體樣式
 
			HSSFCell cell1 = row1.createCell(0);// 創(chuàng)建標(biāo)題第一列
			sheet.addMergedRegion(new CellRangeAddress(0, 0, 0,
					columnNumber - 1)); // 合并列標(biāo)題
			cell1.setCellValue(titleName); // 設(shè)置值標(biāo)題
			cell1.setCellStyle(style2); // 設(shè)置標(biāo)題樣式
 
			// 創(chuàng)建第1行 也就是表頭
			HSSFRow row = sheet.createRow((int) 1);
			row.setHeightInPoints(37);// 設(shè)置表頭高度
 
			// 第四步,創(chuàng)建表頭單元格樣式 以及表頭的字體樣式
			HSSFCellStyle style = wb.createCellStyle();
			style.setWrapText(true);// 設(shè)置自動(dòng)換行
			style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
			style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); // 創(chuàng)建一個(gè)居中格式
 
			style.setBottomBorderColor(HSSFColor.BLACK.index);
			style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
			style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
			style.setBorderRight(HSSFCellStyle.BORDER_THIN);
			style.setBorderTop(HSSFCellStyle.BORDER_THIN);
 
			HSSFFont headerFont = (HSSFFont) wb.createFont(); // 創(chuàng)建字體樣式
			headerFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); // 字體加粗
			headerFont.setFontName("黑體"); // 設(shè)置字體類(lèi)型
			headerFont.setFontHeightInPoints((short) 10); // 設(shè)置字體大小
			style.setFont(headerFont); // 為標(biāo)題樣式設(shè)置字體樣式
 
			// 第四.一步,創(chuàng)建表頭的列
			for (int i = 0; i < columnNumber; i++) 
			{
				HSSFCell cell = row.createCell(i);
				cell.setCellValue(columnName[i]);
				cell.setCellStyle(style);
			}
 
			// 第五步,創(chuàng)建單元格,并設(shè)置值
			for (int i = 0; i < dataList.length; i++) 
			{
				row = sheet.createRow((int) i + 2);
				// 為數(shù)據(jù)內(nèi)容設(shè)置特點(diǎn)新單元格樣式1 自動(dòng)換行 上下居中
				HSSFCellStyle zidonghuanhang = wb.createCellStyle();
				zidonghuanhang.setWrapText(true);// 設(shè)置自動(dòng)換行
				zidonghuanhang.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); // 創(chuàng)建一個(gè)居中格式
 
				// 設(shè)置邊框
				zidonghuanhang.setBottomBorderColor(HSSFColor.BLACK.index);
				zidonghuanhang.setBorderBottom(HSSFCellStyle.BORDER_THIN);
				zidonghuanhang.setBorderLeft(HSSFCellStyle.BORDER_THIN);
				zidonghuanhang.setBorderRight(HSSFCellStyle.BORDER_THIN);
				zidonghuanhang.setBorderTop(HSSFCellStyle.BORDER_THIN);
 
				// 為數(shù)據(jù)內(nèi)容設(shè)置特點(diǎn)新單元格樣式2 自動(dòng)換行 上下居中左右也居中
				HSSFCellStyle zidonghuanhang2 = wb.createCellStyle();
				zidonghuanhang2.setWrapText(true);// 設(shè)置自動(dòng)換行
				zidonghuanhang2
						.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); // 創(chuàng)建一個(gè)上下居中格式
				zidonghuanhang2.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 左右居中
 
				// 設(shè)置邊框
				zidonghuanhang2.setBottomBorderColor(HSSFColor.BLACK.index);
				zidonghuanhang2.setBorderBottom(HSSFCellStyle.BORDER_THIN);
				zidonghuanhang2.setBorderLeft(HSSFCellStyle.BORDER_THIN);
				zidonghuanhang2.setBorderRight(HSSFCellStyle.BORDER_THIN);
				zidonghuanhang2.setBorderTop(HSSFCellStyle.BORDER_THIN);
				HSSFCell datacell = null;
				for (int j = 0; j < columnNumber; j++) 
				{
					datacell = row.createCell(j);
					datacell.setCellValue(dataList[i][j]);
					datacell.setCellStyle(zidonghuanhang2);
				}
			}
 
			// 第六步,將文件存到瀏覽器設(shè)置的下載位置
			String filename = fileName + ".xls";
			response.setContentType("application/ms-excel;charset=UTF-8");
			response.setHeader("Content-Disposition", "attachment;filename="
					.concat(String.valueOf(URLEncoder.encode(filename, "UTF-8"))));
			OutputStream out = response.getOutputStream();
			try {
				wb.write(out);// 將數(shù)據(jù)寫(xiě)出去
				String str = "導(dǎo)出" + fileName + "成功!";
				System.out.println(str);
			} catch (Exception e) {
				e.printStackTrace();
				String str1 = "導(dǎo)出" + fileName + "失??!";
				System.out.println(str1);
			} finally {
				out.close();
			}
 
		} else {
			System.out.println("列數(shù)目長(zhǎng)度名稱(chēng)三個(gè)數(shù)組長(zhǎng)度要一致");
		}
 
	}
 
	public void ExportNoResponse(String sheetName, String titleName,
			String fileName, int columnNumber, int[] columnWidth,
			String[] columnName, String[][] dataList) throws Exception {
		if (columnNumber == columnWidth.length&& columnWidth.length == columnName.length) {
			// 第一步,創(chuàng)建一個(gè)webbook,對(duì)應(yīng)一個(gè)Excel文件
			HSSFWorkbook wb = new HSSFWorkbook();
			// 第二步,在webbook中添加一個(gè)sheet,對(duì)應(yīng)Excel文件中的sheet
			HSSFSheet sheet = wb.createSheet(sheetName);
			// sheet.setDefaultColumnWidth(15); //統(tǒng)一設(shè)置列寬
			for (int i = 0; i < columnNumber; i++) 
			{
				for (int j = 0; j <= i; j++) 
				{
					if (i == j) 
					{
						sheet.setColumnWidth(i, columnWidth[j] * 256); // 單獨(dú)設(shè)置每列的寬
					}
				}
			}
			// 創(chuàng)建第0行 也就是標(biāo)題
			HSSFRow row1 = sheet.createRow((int) 0);
			row1.setHeightInPoints(50);// 設(shè)備標(biāo)題的高度
			// 第三步創(chuàng)建標(biāo)題的單元格樣式style2以及字體樣式headerFont1
			HSSFCellStyle style2 = wb.createCellStyle();
			style2.setAlignment(HSSFCellStyle.ALIGN_CENTER);
			style2.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
			style2.setFillForegroundColor(HSSFColor.LIGHT_TURQUOISE.index);
			style2.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
			HSSFFont headerFont1 = (HSSFFont) wb.createFont(); // 創(chuàng)建字體樣式
			headerFont1.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); // 字體加粗
			headerFont1.setFontName("黑體"); // 設(shè)置字體類(lèi)型
			headerFont1.setFontHeightInPoints((short) 15); // 設(shè)置字體大小
			style2.setFont(headerFont1); // 為標(biāo)題樣式設(shè)置字體樣式
 
			HSSFCell cell1 = row1.createCell(0);// 創(chuàng)建標(biāo)題第一列
			sheet.addMergedRegion(new CellRangeAddress(0, 0, 0,
					columnNumber - 1)); // 合并第0到第17列
			cell1.setCellValue(titleName); // 設(shè)置值標(biāo)題
			cell1.setCellStyle(style2); // 設(shè)置標(biāo)題樣式
 
			// 創(chuàng)建第1行 也就是表頭
			HSSFRow row = sheet.createRow((int) 1);
			row.setHeightInPoints(37);// 設(shè)置表頭高度
 
			// 第四步,創(chuàng)建表頭單元格樣式 以及表頭的字體樣式
			HSSFCellStyle style = wb.createCellStyle();
			style.setWrapText(true);// 設(shè)置自動(dòng)換行
			style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
			style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); // 創(chuàng)建一個(gè)居中格式
 
			style.setBottomBorderColor(HSSFColor.BLACK.index);
			style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
			style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
			style.setBorderRight(HSSFCellStyle.BORDER_THIN);
			style.setBorderTop(HSSFCellStyle.BORDER_THIN);
 
			HSSFFont headerFont = (HSSFFont) wb.createFont(); // 創(chuàng)建字體樣式
			headerFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); // 字體加粗
			headerFont.setFontName("黑體"); // 設(shè)置字體類(lèi)型
			headerFont.setFontHeightInPoints((short) 10); // 設(shè)置字體大小
			style.setFont(headerFont); // 為標(biāo)題樣式設(shè)置字體樣式
 
			// 第四.一步,創(chuàng)建表頭的列
			for (int i = 0; i < columnNumber; i++) 
			{
				HSSFCell cell = row.createCell(i);
				cell.setCellValue(columnName[i]);
				cell.setCellStyle(style);
			}
 
			// 第五步,創(chuàng)建單元格,并設(shè)置值
			for (int i = 0; i < dataList.length; i++) 
			{
				row = sheet.createRow((int) i + 2);
				// 為數(shù)據(jù)內(nèi)容設(shè)置特點(diǎn)新單元格樣式1 自動(dòng)換行 上下居中
				HSSFCellStyle zidonghuanhang = wb.createCellStyle();
				zidonghuanhang.setWrapText(true);// 設(shè)置自動(dòng)換行
				zidonghuanhang
						.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); // 創(chuàng)建一個(gè)居中格式
 
				// 設(shè)置邊框
				zidonghuanhang.setBottomBorderColor(HSSFColor.BLACK.index);
				zidonghuanhang.setBorderBottom(HSSFCellStyle.BORDER_THIN);
				zidonghuanhang.setBorderLeft(HSSFCellStyle.BORDER_THIN);
				zidonghuanhang.setBorderRight(HSSFCellStyle.BORDER_THIN);
				zidonghuanhang.setBorderTop(HSSFCellStyle.BORDER_THIN);
 
				// 為數(shù)據(jù)內(nèi)容設(shè)置特點(diǎn)新單元格樣式2 自動(dòng)換行 上下居中左右也居中
				HSSFCellStyle zidonghuanhang2 = wb.createCellStyle();
				zidonghuanhang2.setWrapText(true);// 設(shè)置自動(dòng)換行
				zidonghuanhang2
						.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); // 創(chuàng)建一個(gè)上下居中格式
				zidonghuanhang2.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 左右居中
 
				// 設(shè)置邊框
				zidonghuanhang2.setBottomBorderColor(HSSFColor.BLACK.index);
				zidonghuanhang2.setBorderBottom(HSSFCellStyle.BORDER_THIN);
				zidonghuanhang2.setBorderLeft(HSSFCellStyle.BORDER_THIN);
				zidonghuanhang2.setBorderRight(HSSFCellStyle.BORDER_THIN);
				zidonghuanhang2.setBorderTop(HSSFCellStyle.BORDER_THIN);
				HSSFCell datacell = null;
				for (int j = 0; j < columnNumber; j++) 
				{
					datacell = row.createCell(j);
					datacell.setCellValue(dataList[i][j]);
					datacell.setCellStyle(zidonghuanhang2);
				}
			}
 
			// 第六步,將文件存到指定位置
			try {
				FileOutputStream fout = new FileOutputStream("D:students.xls");
				wb.write(fout);
				String str = "導(dǎo)出" + fileName + "成功!";
				System.out.println(str);
				fout.close();
			} catch (Exception e) {
				e.printStackTrace();
				String str1 = "導(dǎo)出" + fileName + "失?。?quot;;
				System.out.println(str1);
			}
		} else {
			System.out.println("列數(shù)目長(zhǎng)度名稱(chēng)三個(gè)數(shù)組長(zhǎng)度要一致");
		}
 
	}
 
}

為了本地測(cè)試效果,單獨(dú)寫(xiě)了一個(gè)無(wú)response參數(shù)的ExportNoResponse方法,直接將文件保存到指定目錄D盤(pán)。

兩個(gè)方法的不同就在于第六步中,有response參數(shù)的方法可以將文件存到瀏覽器設(shè)置的下載位置。

下面是導(dǎo)出效果截圖:

使用java怎么導(dǎo)出Excel

Java的特點(diǎn)有哪些

Java的特點(diǎn)有哪些 1.Java語(yǔ)言作為靜態(tài)面向?qū)ο缶幊陶Z(yǔ)言的代表,實(shí)現(xiàn)了面向?qū)ο罄碚?,允許程序員以?xún)?yōu)雅的思維方式進(jìn)行復(fù)雜的編程。 2.Java具有簡(jiǎn)單性、面向?qū)ο蟆⒎植际?、安全性、平臺(tái)獨(dú)立與可移植性、動(dòng)態(tài)性等特點(diǎn)。 3.使用Java可以編寫(xiě)桌面應(yīng)用程序、Web應(yīng)用程序、分布式系統(tǒng)和嵌入式系統(tǒng)應(yīng)用程序等。

上述就是小編為大家分享的使用java怎么導(dǎo)出Excel了,如果剛好有類(lèi)似的疑惑,不妨參照上述分析進(jìn)行理解。如果想知道更多相關(guān)知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI