javaは複数のsheetを含むExcelコードの例をエクスポートします。
本論文の例では、Javaが複数のsheetを含むExcelの具体的なコードを共有しています。参考にしてください。具体的な内容は以下の通りです。
複数のsheetをエクスポートするには、Excelから導出された時間設定がポイントです。エクスポートファイルを実行する前に、複数のシートを作成します。
例:
Javaクラス:
複数のsheetをエクスポートするには、Excelから導出された時間設定がポイントです。エクスポートファイルを実行する前に、複数のシートを作成します。
HSSFSheet sheet = workbook.createSheet(sheettitle);
このように、シートを作成するごとに、新しいsheet表が作成され、最後にExcelをエクスポートする時に一括でエクスポートされます。例:
Javaクラス:
try {
HSSFWorkbook workbook = new HSSFWorkbook();
OutputStream out = response.getOutputStream();
for(int j=0;j<n;j++){
BaseResult<List<T>> teasalList = service.select(teasal);
// list Excel
if(teasalList.isSuccess()&&teasalList.getResult().size()>0){
//
SimpleDateFormat formatter1 = new SimpleDateFormat("yyyy-MM-dd");
String nowdate = formatter1.format(new Date());
String title = null;
title = "excel -" + nowdate + ".xls";
String sheettitle = "sheet ";
//
String oneheaders = " " ;
String dateheaders = nowdate ;
String[] headers = new String[] {" 1"," 2"," 3"," 4"};
List<Object[]> dataList = new ArrayList<Object[]>();
Object[] objs = null;
for(int i =0; i<3 ; i++){ //
objs = new Object[headers.length];
objs[1] = " "; //
objs[2] = "3"; //
// excel
dataList.add(objs);
}
//
//
String headStr = "attachment; filename=\"" + new String( title.getBytes("gb2312"), "ISO8859-1" ) + "\"";
response.setContentType("octets/stream");
response.setContentType("APPLICATION/OCTET-STREAM");
response.setHeader("Content-Disposition", headStr);
ExportExcelDownFee ex ;
ex = new ExportExcelDownFee(sheettitle, oneheaders, dateheaders,headers, dataList);//
ex.export(workbook,out);
}
}
workbook.write(out); // sheet Excel
out.close(); //
} catch (Exception e) {
e.printStackTrace();
}
ツールクラス:
public class ExportExcelDownFee {
//
private String[] rowName ;
//
private String oneheaders;
//
private String dateheaders;
//sheet
private String sheettitle;
private List<Object[]> dataList = new ArrayList<Object[]>();
HttpServletResponse response;
// 2,
public ExportExcelDownFee( String sheettitle, String oneheaders, String dateheaders, String[] rowName,List<Object[]> dataList){
this.dataList = dataList;
this.oneheaders = oneheaders;
this.dateheaders = dateheaders;
this.rowName = rowName;
this.sheettitle = sheettitle;
}
/*
*
* */
public void export(HSSFWorkbook workbook,OutputStream out) throws Exception{
try{
HSSFSheet sheet = workbook.createSheet(sheettitle); //
HSSFCellStyle columnTopStyle = this.getColumnTopStyle(workbook);//
HSSFCellStyle style = this.getStyle(workbook); //
//
HSSFRow rowfirstName = sheet.createRow(0);
HSSFCell oneCellRowName = rowfirstName.createCell(0); //
oneCellRowName.setCellType(HSSFCell.CELL_TYPE_STRING); //
HSSFRichTextString onetext = new HSSFRichTextString(oneheaders);
oneCellRowName.setCellValue(onetext); //
// CellRangeAddress , , ,
sheet.addMergedRegion(new CellRangeAddress(0,0,0,3));
oneCellRowName.setCellStyle(columnTopStyle); //
//
HSSFRow rowDateName = sheet.createRow(1);
HSSFCell DateCellRowName = rowDateName.createCell(3);
DateCellRowName.setCellValue(dateheaders);
DateCellRowName.setCellStyle(columnTopStyle);
//
int columnNum = rowName.length;
HSSFRow rowRowName = sheet.createRow(2); // 2 ( )
// sheet
for(int n=0;n<columnNum;n++){
HSSFCell cellRowName = rowRowName.createCell(n); //
cellRowName.setCellType(HSSFCell.CELL_TYPE_STRING); //
HSSFRichTextString text = new HSSFRichTextString(rowName[n]);
cellRowName.setCellValue(text); //
cellRowName.setCellStyle(style); //
}
// sheet
for(int i=0;i<dataList.size();i++){
Object[] obj = dataList.get(i);//
HSSFRow row = sheet.createRow(i+3);// ( )
for(int j=0; j<obj.length; j++){
HSSFCell cell = null; //
if(j == 0){
cell = row.createCell(j,HSSFCell.CELL_TYPE_NUMERIC);
cell.setCellValue(i+1);
}else{
cell = row.createCell(j,HSSFCell.CELL_TYPE_STRING);
if(!"".equals(obj[j]) && obj[j] != null){
cell.setCellValue(obj[j].toString()); //
}
}
cell.setCellStyle(style); //
}
}
//
for (int colNum = 0; colNum < columnNum; colNum++) {
int columnWidth = sheet.getColumnWidth(colNum) / 256;
for (int rowNum = 0; rowNum < sheet.getLastRowNum(); rowNum++) {
HSSFRow currentRow;
//
if (sheet.getRow(rowNum) == null) {
currentRow = sheet.createRow(rowNum);
} else {
currentRow = sheet.getRow(rowNum);
}
if (currentRow.getCell(colNum) != null) {
HSSFCell currentCell = currentRow.getCell(colNum);
if (currentCell.getCellType() == HSSFCell.CELL_TYPE_STRING) {
int length = 0;
try {
length = currentCell.getStringCellValue().getBytes().length;
} catch (Exception e) {
e.printStackTrace();
}
if (columnWidth < length) {
columnWidth = length;
}
}
}
}
if(colNum == 0){
sheet.setColumnWidth(colNum, (columnWidth-2) * 256);
}else{
sheet.setColumnWidth(colNum, (columnWidth+4) * 256);
}
}
}catch(Exception e){
e.printStackTrace();
}
}
/*
*
*/
public HSSFCellStyle getColumnTopStyle(HSSFWorkbook workbook) {
//
HSSFFont font = workbook.createFont();
//
font.setFontHeightInPoints((short)11);
//
//font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
//
font.setFontName(" ");
// ;
HSSFCellStyle style = workbook.createCellStyle();
// ;
style.setFont(font);
// ;
style.setWrapText(false);
// ;
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
// ;
style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
return style;
}
/*
*
*/
public HSSFCellStyle getStyle(HSSFWorkbook workbook) {
//
HSSFFont font = workbook.createFont();
//
font.setFontName(" ");
// ;
HSSFCellStyle style = workbook.createCellStyle();
// ;
style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
// ;
style.setBottomBorderColor(HSSFColor.BLACK.index);
// ;
style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
// ;
style.setLeftBorderColor(HSSFColor.BLACK.index);
// ;
style.setBorderRight(HSSFCellStyle.BORDER_THIN);
// ;
style.setRightBorderColor(HSSFColor.BLACK.index);
// ;
style.setBorderTop(HSSFCellStyle.BORDER_THIN);
// ;
style.setTopBorderColor(HSSFColor.BLACK.index);
// ;
style.setFont(font);
// ;
style.setWrapText(false);
// ;
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
// ;
style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
return style;
}
}
以上述べたのは小编で绍介したjavaです。複数のsheetを含むExcelコードの例を导いて整理します。皆さんに助けてほしいです。もし何か質問があれば、メッセージをください。小编はすぐに返事します。ここでも私たちのサイトを応援してくれてありがとうございます。