后附:如果工作时写的代码也像上面那样,是要挨骂的。作为程序员应该痛恨写重复的代码,下面为大家付上整理版(当然下面的代码还有很多可以改进的地方,比如把方法做得更灵活,让更多可以以活动设置的东西作为参数传进来就可以了):
package
com.hlm.java.excel;
import
java.io.FileNotFoundException;
import
java.io.FileOutputStream;
import
java.io.IOException;
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.ss.util.CellRangeAddress;
public
class
ExportExcel {
public
static
void
main(String[]
args
) {
HSSFWorkbook
wb
=
new
HSSFWorkbook();
//创建excel的文档对象
HSSFSheet
sheet
=
wb
.createSheet();
//创建excel的表单
//设置工作表名称
wb
.setSheetName(0,
"测试表头"
);
//表单名称
HSSFCellStyle
sheetStyle
=
getStyle
(
wb
);
int
b
= 3;
//设置列宽度
sheet
.setColumnWidth(0, 838*
b
);
sheet
.setColumnWidth(1, 838*
b
);
sheet
.setColumnWidth(2, 838*
b
);
sheet
.setColumnWidth(3, 838*
b
);
HSSFRow
row0
=
sheet
.createRow((
short
)0);
//excel的行
row0
.setHeightInPoints(40);
buildCell
(
sheet
,
row0
,
sheetStyle
,
"测试表头"
,0,0,0,3,0);
HSSFRow
row1
=
sheet
.createRow(1);
//设置行高
row1
.setHeight((
short
) 800);
buildCell
(
sheet
,
row1
,
sheetStyle
,
"序号"
,1,2,0,0,0);
buildCell
(
sheet
,
row1
,
sheetStyle
,
"姓名"
,1,2,1,1,1);
buildCell
(
sheet
,
row1
,
sheetStyle
,
"身份信息"
,1,1,2,3,2);
HSSFRow
row2
=
sheet
.createRow(2);
//设置行高
row2
.setHeight((
short
) 800);
buildCell
(
sheet
,
row2
,
sheetStyle
,
"性别"
,2,2,2,2,2);
buildCell
(
sheet
,
row2
,
sheetStyle
,
"出生年月"
,2,2,3,3,3);
HSSFRow
row3
=
sheet
.createRow(3);
//设置行高
row3
.setHeight((
short
) 800);
buildCell
(
sheet
,
row3
,
sheetStyle
,
"1"
,3,3,0,0,0);
buildCell
(
sheet
,
row3
,
sheetStyle
,
"小明"
,3,3,1,1,1);
buildCell
(
sheet
,
row3
,
sheetStyle
,
"男"
,3,3,2,2,2);
buildCell
(
sheet
,
row3
,
sheetStyle
,
"201801"
,3,3,3,3,3);
writeOutPutStream
(
wb
,
"E:\\workspace\\测试Excel.xls"
);
}
/**
* 获取样式:“宋体”、14号、居中显示
*
@param
wb
*
@return
*/
private
static
HSSFCellStyle getStyle(HSSFWorkbook
wb
){
//设置样式
HSSFCellStyle
sheetStyle
=
wb
.createCellStyle();
//cell样式
HSSFFont
font
=
wb
.createFont();
//excel字体
font
.setFontName(
"宋体"
);
// 字体
font
.setFontHeightInPoints((
short
)14);
// 字体高度
font
.setBoldweight(HSSFFont.
BOLDWEIGHT_BOLD
);
// 宽度
sheetStyle
.setFont(
font
);
sheetStyle
.setAlignment(HSSFCellStyle.
ALIGN_CENTER
);
//横向居中
sheetStyle
.setVerticalAlignment(HSSFCellStyle.
VERTICAL_CENTER
);
// 纵向居中
return
sheetStyle
;
}
/**
* 构建单元格
*
@param
sheet 工作簿对象
*
@param
row 行对象
*
@param
style 样式对象
*
@param
value 单元格中值
*
@param
firstRow 单元格占起始行
*
@param
lastRow 单元格占结束行
*
@param
firstCol 单元格占起始列
*
@param
lastCol 单元格占结束列
*
@param
cellIdex 该行的第几个单元格(多行合并的取首个)
*/
private
static
void
buildCell(HSSFSheet
sheet
, HSSFRow
row
, HSSFCellStyle
style
, String
value
,
int
firstRow
,
int
lastRow
,
int
firstCol
,
int
lastCol
,
int
cellIdex
){
HSSFCell
cell
=
row
.createCell(
cellIdex
);
cell
.setCellStyle(
style
);
cell
.setCellValue(
value
);
sheet
.addMergedRegion(
new
CellRangeAddress(
firstRow
,
lastRow
,
firstCol
,
lastCol
));
}
/**
* 获取输出流并将Excel 导出
*
@param
wb
*
@param
dirAndFilesName
*
@return
*/
private
static
FileOutputStream writeOutPutStream(HSSFWorkbook
wb
,String
dirAndFilesName
){
FileOutputStream
output
=
null
;
try
{
output
=
new
FileOutputStream(
dirAndFilesName
);
}
catch
(FileNotFoundException
e
) {
//
TODO
Auto-generated catch block
e
.printStackTrace();
}
try
{
wb
.write(
output
);
}
catch
(IOException
e
) {
//
TODO
Auto-generated catch block
e
.printStackTrace();
}
try
{
output
.flush();
output
.close();
}
catch
(IOException
e
) {
//
TODO
Auto-generated catch block
e
.printStackTrace();
}
return
output
;
}
}