创建一个新的Excle工作薄view plain // 创建新的Excel 工作簿HSSFWorkbook workbook = new HSSFWorkbook()
// 在Excel工作簿中建一工作表其名为缺省值 也可以指定Sheet名称HSSFSheet sheet = workbookcreateSheet()//HSSFSheet sheet = workbookcreateSheet(SheetName)
创建新行(row)并将单元格(cell)放入其中 行号从开始计算
view plain HSSFRow row = sheetcreateRow((short) )设置sheet名称和单元格内容为中文view plain wbsetSheetName(n 中文HSSFCellENCODING_UTF_)cellsetEncoding((short) )cellsetCellValue(中文)设置列宽行高view plain sheetsetColumnWidth((short)column(short)width)rowsetHeight((short)height)添加区域合并单元格view plain Region region = new Region((short)rowFrom(short)columnFrom(short)rowTo(short)columnTo)sheetaddMergedRegion(region)//得到所有区域sheetgetNumMergedRegions()
设置单元格边框格式虚线HSSFCellStyleBORDER_DOTTED实线HSSFCellStyleBORDER_THIN view plain public static HSSFCellStyle getCellStyle(short type){ HSSFWorkbook wb = new HSSFWorkbook()HSSFCellStyle style = wbcreateCellStyle()stylesetBorderBottom(type)//下边框stylesetBorderLeft(type)//左边框stylesetBorderRight(type)//右边框stylesetBorderTop(type)//上边框return style} 设置字体和内容位置view plain HSSFFont f = wbcreateFont()fsetFontHeightInPoints((short) )//字号fsetBoldweight(HSSFFontBOLDWEIGHT_NORMAL)//加粗stylesetFont(f)stylesetAlignment(HSSFCellStyleALIGN_CENTER)//左右居中stylesetVerticalAlignment(HSSFCellStyleVERTICAL_CENTER)//上下居中stylesetRotation(short rotation)//单元格内容的旋转的角度HSSFDataFormat df = wbcreateDataFormat()stylesetDataFormat(dfgetFormat(%))//设置单元格数据格式cellsetCellFormula(string)//给单元格设公式stylesetRotation(short rotation)//单元格内容的旋转的角度cellsetCellStyle(style)插入图片view plain //先把读进来的图片放到一个ByteArrayOutputStream中以便产生ByteArray ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream()BufferedImage bufferImg = ImageIOread(new File(okjpg))ImageIOwrite(bufferImgjpgbyteArrayOut)//读进一个excel模版FileInputStream fos = new FileInputStream(filePathName+/stencilxlt)fs = new POIFSFileSystem(fos)//创建一个工作薄HSSFWorkbook wb = new HSSFWorkbook(fs)HSSFSheet sheet = wbgetSheetAt()HSSFPatriarch patriarch = sheetcreateDrawingPatriarch()HSSFClientAnchor anchor = new HSSFClientAnchor((short) (short))patriarchcreatePicture(anchor wbaddPicture(byteArrayOuttoByteArray()HSSFWorkbookPICTURE_TYPE_JPEG))
例java view plain import javaioFileOutputStream
import orgapachepoihssfusermodelHSSFCellimport orgapachepoihssfusermodelHSSFCellStyleimport orgapachepoihssfusermodelHSSFFontimport orgapachepoihssfusermodelHSSFRowimport orgapachepoihssfusermodelHSSFSheetimport orgapachepoihssfusermodelHSSFWorkbookimport orgapachepoihssfusermodelHSSFRichTextStringimport orgapachepoihssfusermodelHSSFDataFormatimport orgapachepoihssfusermodelHSSFCommentimport orgapachepoihssfusermodelHSSFPatriarchimport orgapachepoihssfusermodelHSSFClientAnchor
public class PoiCreateExcelTest ……{ public static void main(String[] args) ……{ /** *//** * @see <a >>For more</a> */ // 创建新的Excel 工作簿HSSFWorkbook workbook = new HSSFWorkbook()
// 在Excel工作簿中建一工作表其名为缺省值 也可以指定Sheet名称HSSFSheet sheet = workbookcreateSheet()//HSSFSheet sheet = workbookcreateSheet(SheetName)
// 用于格式化单元格的数据HSSFDataFormat format = workbookcreateDataFormat()
// 创建新行(row)并将单元格(cell)放入其中 行号从开始计算
HSSFRow row = sheetcreateRow((short) )
// 设置字体HSSFFont font = workbookcreateFont()fontsetFontHeightInPoints((short) ) //字体高度fontsetColor(HSSFFontCOLOR_RED) //字体颜色fontsetFontName(黑体) //字体fontsetBoldweight(HSSFFontBOLDWEIGHT_BOLD) //宽度fontsetItalic(true) //是否使用斜体// fontsetStrikeout(true) //是否使用划线
// 设置单元格类型HSSFCellStyle cellStyle = workbookcreateCellStyle()cellStylesetFont(font)cellStylesetAlignment(HSSFCellStyleALIGN_CENTER) //水平布局居中cellStylesetWrapText(true)
// 添加单元格注释// 创建HSSFPatriarch对象HSSFPatriarch是所有注释的容器
HSSFPatriarch patr = sheetcreateDrawingPatriarch()// 定义注释的大小和位置详见文档HSSFComment comment = patrcreateComment(new HSSFClientAnchor( (short) (short) ))// 设置注释内容commentsetString(new HSSFRichTextString(可以在POI中添加注释!))// 设置注释作者 当鼠标移动到单元格上是可以在状态栏中看到该内容
commentsetAuthor(Xuys)
// 创建单元格HSSFCell cell = rowcreateCell((short) )HSSFRichTextString hssfString = new HSSFRichTextString(Hello World!)cellsetCellValue(hssfString)//设置单元格内容cellsetCellStyle(cellStyle)//设置单元格样式cellsetCellType(HSSFCellCELL_TYPE_STRING)//指定单元格格式数值公式或字符串cellsetCellComment(comment)//添加注释
//格式化数据row = sheetcreateRow((short) )cell = rowcreateCell((short) )cellsetCellValue()cellStyle = workbookcreateCellStyle()cellStylesetDataFormat(formatgetFormat())cellsetCellStyle(cellStyle)
row = sheetcreateRow((short) )cell = rowcreateCell((short) )cellsetCellValue()cellStyle = workbookcreateCellStyle()cellStylesetDataFormat(formatgetFormat(###))cellsetCellStyle(cellStyle)
sheetautoSizeColumn((short)) //调整第一列宽度sheetautoSizeColumn((short)) //调整第二列宽度sheetautoSizeColumn((short)) //调整第三列宽度sheetautoSizeColumn((short)) //调整第四列宽度
try ……{ FileOutputStream fileOut = new FileOutputStream(C\xls)workbookwrite(fileOut)fileOutclose()} catch (Exception e) ……{ Systemoutprintln(etoString())}
}如果编译器提示没有autoSizeColumn这个方法那可能是你的poi版本太低的缘故我用的是poi版本
例Grails view plain /** * Excel导出方法导出班级工作情况统计列表* @param * titleList 标题集合* classTableInstanceListquestionList 数据集合* * out OutputStream out = responsegetOutputStream()* @return * */ def exportWorkList(titleListclassListquestionListoutttList){
Workbook wb = new HSSFWorkbook()Sheet sheet = wbcreateSheet(sheet) //班级工作情况统计
HSSFRow row = sheetcreateRow((short))HSSFRow row = sheetcreateRow((short))HSSFCell cell = rowcreateCell((short))HSSFCell cell = rowcreateCell((short))
// 设置字体HSSFFont font = wbcreateFont()//fontsetFontHeightInPoints((short) ) //字体高度//fontsetColor(HSSFFontCOLOR_RED) //字体颜色//fontsetFontName(黑体) //字体fontsetBoldweight(HSSFFontBOLDWEIGHT_BOLD) //宽度
// 设置单元格类型HSSFCellStyle cellStyle = wbcreateCellStyle()cellStylesetFont(font)cellStylesetAlignment(HSSFCellStyleALIGN_CENTER) //水平布局居中cellStylesetWrapText(true)
HSSFCellStyle cellStyle = wbcreateCellStyle()cellStylesetAlignment(HSSFCellStyleVERTICAL_TOP) //垂直布局居上cellStylesetWrapText(true) //设置自动换行
//设置单元格宽度sheetsetColumnWidth() //序列sheetsetColumnWidth() //区县名称sheetsetColumnWidth() //基地名称sheetsetColumnWidth() //班级名称sheetsetColumnWidth() //班级状态
sheetsetColumnWidth() //拓宽问题的单元格宽度
sheetsetColumnWidth() //评价最高的老师sheetsetColumnWidth() //评价最低的老师
titleListeachWithIndex {pi> if(i>= && i<=){ cell = rowcreateCell(+*(i))sheetaddMergedRegion(new Region((short)(short)))//合并区域第一行sheetaddMergedRegion(new Region((short)(+*(i))(short)(+*(i)+)))//指定合并区域cellsetCellValue(p)cellsetCellStyle(cellStyle)//设置单元格样式
} } }
try { wbwrite(out)outclose()} catch (FileNotFoundException e) { //eprintStackTrace()}catch(IOException e){ //eprintStackTrace()}