public HSSFWorkbook readExcelTemplate(InputStream inputStream,
String sheetName) throws FileNotFoundException
{
HSSFWorkbook wb = null;
try
{
wb = new HSSFWorkbook(new POIFSFileSystem(inputStream));
HSSFSheet sheet = null;
sheet = wb.getSheet(sheetName);
if (sheet == null)
sheet = wb.getSheetAt(0);
if (sheet == null)
sheet = wb.createSheet(sheetName);
}
catch (IOException e)
{
e.printStackTrace();
}
finally
{
try
{
if(inputStream != null)
{
inputStream.close();
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
return wb;
}
/**
* 写Excel表到文件。
* @param excel
* :Excel对象
* @param sheetName:Excel
* 页签
* @param outfile:输出文件名
* @return
* @throws FileNotFoundException
* @throws FileNotFoundException
*/
public void writeExcelOutputFile(HSSFWorkbook excel,
String outfile) throws FileNotFoundException,IOException {
OutputStream fout = new FileOutputStream(outfile);
HSSFWorkbook wb = excel;
if (wb == null)
return;
try {
wb.write(fout);
} catch (IOException e) {
throw e;
} finally {
fout.close();
}
}
String sheetName) throws FileNotFoundException
{
HSSFWorkbook wb = null;
try
{
wb = new HSSFWorkbook(new POIFSFileSystem(inputStream));
HSSFSheet sheet = null;
sheet = wb.getSheet(sheetName);
if (sheet == null)
sheet = wb.getSheetAt(0);
if (sheet == null)
sheet = wb.createSheet(sheetName);
}
catch (IOException e)
{
e.printStackTrace();
}
finally
{
try
{
if(inputStream != null)
{
inputStream.close();
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
return wb;
}
/**
* 写Excel表到文件。
* @param excel
* :Excel对象
* @param sheetName:Excel
* 页签
* @param outfile:输出文件名
* @return
* @throws FileNotFoundException
* @throws FileNotFoundException
*/
public void writeExcelOutputFile(HSSFWorkbook excel,
String outfile) throws FileNotFoundException,IOException {
OutputStream fout = new FileOutputStream(outfile);
HSSFWorkbook wb = excel;
if (wb == null)
return;
try {
wb.write(fout);
} catch (IOException e) {
throw e;
} finally {
fout.close();
}
}