자바 엑셀 내 보 내기 일반적인 방법 인 스 턴 스
우선 도입 해 야 할 jar 패키지:
그리고 정식 코드 야.
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 = " ";
String titleName = " ";
String fileName = " ";
int columnNumber = 3;
int[] columnWidth = { 10, 20, 30 };
String[][] dataList = { { "001", "2015-01-01", "IT" },
{ "002", "2015-01-02", " " }, { "003", "2015-01-03", " " } };
String[] columnName = { " ", " ", " " };
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) {
// , webbook, Excel
HSSFWorkbook wb = new HSSFWorkbook();
// , webbook sheet, Excel sheet
HSSFSheet sheet = wb.createSheet(sheetName);
// sheet.setDefaultColumnWidth(15); //
for (int i = 0; i < columnNumber; i++)
{
for (int j = 0; j <= i; j++)
{
if (i == j)
{
sheet.setColumnWidth(i, columnWidth[j] * 256); //
}
}
}
// 0
HSSFRow row1 = sheet.createRow((int) 0);
row1.setHeightInPoints(50);//
// 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(); //
headerFont1.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); //
headerFont1.setFontName(" "); //
headerFont1.setFontHeightInPoints((short) 15); //
style2.setFont(headerFont1); //
HSSFCell cell1 = row1.createCell(0);//
sheet.addMergedRegion(new CellRangeAddress(0, 0, 0,
columnNumber - 1)); //
cell1.setCellValue(titleName); //
cell1.setCellStyle(style2); //
// 1
HSSFRow row = sheet.createRow((int) 1);
row.setHeightInPoints(37);//
// ,
HSSFCellStyle style = wb.createCellStyle();
style.setWrapText(true);//
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); //
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(); //
headerFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); //
headerFont.setFontName(" "); //
headerFont.setFontHeightInPoints((short) 10); //
style.setFont(headerFont); //
// . ,
for (int i = 0; i < columnNumber; i++)
{
HSSFCell cell = row.createCell(i);
cell.setCellValue(columnName[i]);
cell.setCellStyle(style);
}
// , ,
for (int i = 0; i < dataList.length; i++)
{
row = sheet.createRow((int) i + 2);
// 1
HSSFCellStyle zidonghuanhang = wb.createCellStyle();
zidonghuanhang.setWrapText(true);//
zidonghuanhang.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); //
//
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);
// 2
HSSFCellStyle zidonghuanhang2 = wb.createCellStyle();
zidonghuanhang2.setWrapText(true);//
zidonghuanhang2
.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); //
zidonghuanhang2.setAlignment(HSSFCellStyle.ALIGN_CENTER);//
//
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);
}
}
// ,
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);//
String str = " " + fileName + " !";
System.out.println(str);
} catch (Exception e) {
e.printStackTrace();
String str1 = " " + fileName + " !";
System.out.println(str1);
} finally {
out.close();
}
} else {
System.out.println(" ");
}
}
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) {
// , webbook, Excel
HSSFWorkbook wb = new HSSFWorkbook();
// , webbook sheet, Excel sheet
HSSFSheet sheet = wb.createSheet(sheetName);
// sheet.setDefaultColumnWidth(15); //
for (int i = 0; i < columnNumber; i++)
{
for (int j = 0; j <= i; j++)
{
if (i == j)
{
sheet.setColumnWidth(i, columnWidth[j] * 256); //
}
}
}
// 0
HSSFRow row1 = sheet.createRow((int) 0);
row1.setHeightInPoints(50);//
// 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(); //
headerFont1.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); //
headerFont1.setFontName(" "); //
headerFont1.setFontHeightInPoints((short) 15); //
style2.setFont(headerFont1); //
HSSFCell cell1 = row1.createCell(0);//
sheet.addMergedRegion(new CellRangeAddress(0, 0, 0,
columnNumber - 1)); // 0 17
cell1.setCellValue(titleName); //
cell1.setCellStyle(style2); //
// 1
HSSFRow row = sheet.createRow((int) 1);
row.setHeightInPoints(37);//
// ,
HSSFCellStyle style = wb.createCellStyle();
style.setWrapText(true);//
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); //
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(); //
headerFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); //
headerFont.setFontName(" "); //
headerFont.setFontHeightInPoints((short) 10); //
style.setFont(headerFont); //
// . ,
for (int i = 0; i < columnNumber; i++)
{
HSSFCell cell = row.createCell(i);
cell.setCellValue(columnName[i]);
cell.setCellStyle(style);
}
// , ,
for (int i = 0; i < dataList.length; i++)
{
row = sheet.createRow((int) i + 2);
// 1
HSSFCellStyle zidonghuanhang = wb.createCellStyle();
zidonghuanhang.setWrapText(true);//
zidonghuanhang
.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); //
//
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);
// 2
HSSFCellStyle zidonghuanhang2 = wb.createCellStyle();
zidonghuanhang2.setWrapText(true);//
zidonghuanhang2
.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); //
zidonghuanhang2.setAlignment(HSSFCellStyle.ALIGN_CENTER);//
//
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 = " " + fileName + " !";
System.out.println(str);
fout.close();
} catch (Exception e) {
e.printStackTrace();
String str1 = " " + fileName + " !";
System.out.println(str1);
}
} else {
System.out.println(" ");
}
}
}
로 컬 테스트 효 과 를 위해 response 인자 가 없 는 ExportNoResponse 방법 을 따로 써 서 지정 한 디 렉 터 리 D 디스크 에 파일 을 저장 합 니 다.두 가지 방법 은 여섯 번 째 단계 에서 response 매개 변수 가 있 는 방법 으로 브 라 우 저 설정 의 다운로드 위치 에 파일 을 저장 할 수 있 습 니 다.
다음은 내 보 내기 효과 캡 처:
위 에서 말 한 것 은 소 편 이 여러분 에 게 소개 한 자바 내 보 내기 엑셀 방법 에 대한 상세 한 해석 과 통합 입 니 다.여러분 에 게 도움 이 되 기 를 바 랍 니 다.궁금 한 점 이 있 으 시 면 저 에 게 메 시 지 를 남 겨 주세요.소 편 은 제때에 답 해 드 리 겠 습 니 다.여기 서도 저희 사이트 에 대한 여러분 의 지지 에 감 사 드 립 니 다!
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Is Eclipse IDE dying?In 2014 the Eclipse IDE is the leading development environment for Java with a market share of approximately 65%. but ac...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.