我用java和poi编写了导出数据为excel表的,但不懂如何设置poi的保存路径,请求大虾们帮助。
Java codetry
{
Class.forName(driverName);
}
catch(ClassNotFoundException e)
{
System.out.println("加载驱动程序失败!");
}
try
{
con=DriverManager.getConnection(dbURL,userName,userPwd);
// Statement sql=con.createStatement();
}
catch(Exception e)
{
System.out.println("初始化数据库连接异常..." + e.getMessage());
}
}
public void execExport()
{
// System.out.println();
System.out.println("开始导出数据库......");
try
{
this.exportTable("1","");
this.exportTable("2","" );
}
catch (Exception e)
{
System.out.println("检索库中所有表时异常..." + e.getMessage());
}
}
public void exportTable(String page,String sql)
{
System.out.println("正在导出表:pinspect");
try
{
Statement selectTable = con.createStatement();
sql="select * from '"+sjk.toString()+"'";
ResultSet rset = selectTable.executeQuery(sql);
this.p = page;
this.writeTable(rset, "客户信息"+page+".xls");
}
catch (Exception e)
{
System.out.println("导出表时异常..." + e.getMessage());
}
}
private void writeTable(ResultSet rs, String xlsName)
{
try
{
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = workbook.createSheet();
// workbook.setSheetName(0,xlsName,HSSFWorkbook.ENCODING_UTF_16);
workbook.setSheetName(0, "分页" + String.valueOf(0));
// (short) HSSFWorkbook.ENCODING_UTF_16);
HSSFRow row = sheet.createRow((short) 0);
HSSFCell cell = null;
ResultSetMetaData md = rs.getMetaData();
// 写字段
int nColumn = md.getColumnCount();
for (int i = 1; i <= nColumn; i++)
{
cell = row.createCell((short) (i - 1));
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
// cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue(md.getColumnLabel(i)+"您好");
switch (i)
{
case 1 :cell.setCellValue("请求的编号");
break;
case 2 :cell.setCellValue("当前请求的执行方法数量");
break;
case 3 :cell.setCellValue("当前请求的执行方法总时间");
break;
case 4 :cell.setCellValue("当前请求的执行方法平均时间");
break;
}
}
// 写记录
int iRow = 1;
String uuidName = null;
while (rs.next())
{
row = sheet.createRow((short) iRow);
for (int j = 1; j <= nColumn; j++)
{
cell = row.createCell((short) (j - 1));
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
// cell.setEncoding(HSSFCell.ENCODING_UTF_16);
String val = null;
if(this.p.equals("1"))
{
if(j ==1)
{
if(rs.getObject(j).toString().equals(uuidName))
{
val = " ";
}
else
{
uuidName = rs.getObject(j).toString();
val = uuidName;
}
}
else if(j == 2)
{
val = rs.getObject(j).toString().split("classes/")[1];
}
else
{
val = rs.getObject(j).toString();
}
}else if(j == 1)
{
val = rs.getObject(j).toString().split("classes/")[1];