Workbook workbook = getWorkbook(is);
int num = workbook.getNumberOfSheets();
List<Sheet> sheetList = new ArrayList<Sheet>();
for (int k = 0; k < num; k++)
{
sheetList.add(workbook.getSheetAt(k));
}
for (Sheet sheet : sheetList)
{
String sheetName = sheet.getSheetName();
Row row = sheet.getRow(0);
Cell cell = row.getCell(0);
cell.setCellValue(String.valueOf("有限公司"+year+"年需求表"));
}
ByteArrayOutputStream bos = new ByteArrayOutputStream();
workbook.write(bos);
byte[] brray = bos.toByteArray();
InputStream returnIs = new ByteArrayInputStream(brray);
return returnIs;
//获取wookBook
public static Workbook getWorkbook(InputStream inputStream) throws Exception
{
if (inputStream == null)
{
throw new Exception("上传文件无效,请重新选择文件!");
}
Workbook workbook = null;
try
{
//判断输入流是否支持mark()和reset()方法
if (!inputStream.markSupported())
{
inputStream = new PushbackInputStream(inputStream, 8);
}
//判断文件是否为excel03文件
if (POIFSFileSystem.hasPOIFSHeader(inputStream))
{
workbook = new HSSFWorkbook(inputStream);
}
//判断文件是否为excel07文件
else if (POIXMLDocument.hasOOXMLHeader(inputStream))
{
workbook = new XSSFWorkbook(OPCPackage.open(inputStream));
}
if (workbook == null)
{
throw new Exception("文件错误,导入的不是标准Excel文件!");
}
} catch (Exception e)
{
e.printStackTrace();
}
return workbook;
}
设置excel的表头
最新推荐文章于 2024-06-30 04:08:51 发布