protected void exportAward(DataSet dsResult)
{
if (dsResult != null)
{
string fileName = System.Web.HttpUtility.UrlEncode("任务列表", System.Text.Encoding.UTF8) + ".xls";//excel 名称
ExportDataSetToExcel(dsResult, fileName, "writered");
}
}
/// <summary>
///
/// </summary>
/// <param name="sourceDs">导出数据源</param>
/// <param name="fileName">excel名称</param>
/// <param name="sheetName">工作表名</param>
public void ExportDataSetToExcel(DataSet sourceDs, string fileName, string sheetName)
{
MemoryStream ms = DataSetToExcel(sourceDs, sheetName) as MemoryStream;//创建内存流
HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + fileName);
HttpContext.Current.Response.BinaryWrite(ms.ToArray());
HttpContext.Current.Response.End();
ms.Close();
ms = null;
}
public Stream DataSetToExcel(DataSet sourceDs, string sheetName)
{
HSSFWorkbook workbook = new HSSFWorkbook();
MemoryStream ms = new MemoryStream(); //创建内存流
string[] sheetNames = sheetName.Split(',');
for (int i = 0; i < sheetNames.Length; i++)
{
HSSFSheet sheet = (HSSFSheet)workbook.CreateSheet(sheetNames[i]);
HSSFRow headerRow = (HSSFRow)sheet.CreateRow(0);
//表头
headerRow.CreateCell(0).SetCellValue("序号");
headerRow.CreateCell(1).SetCellValue("任务标题");
headerRow.CreateCell(2).SetCellValue("发布人");
headerRow.CreateCell(3).SetCellValue("发布时间");
headerRow.CreateCell(4).SetCellValue("参阅人");
headerRow.CreateCell(5).SetCellValue("接收范围");
headerRow.CreateCell(6).SetCellValue("任务时间");
headerRow.CreateCell(7).SetCellValue("任务状态");
//填充表格
int rowIndex = 1;
for (int j = 0; j < sourceDs.Tables[0].Rows.Count; j++)
{
HSSFRow dataRow = (HSSFRow)sheet.CreateRow(rowIndex);
//for (int a = 0; a < sourceDs.Tables[0].Columns.Count; a++)
//{
dataRow.CreateCell(0).SetCellValue(sourceDs.Tables[0].Rows[j]["Row"].ToString());
dataRow.CreateCell(1).SetCellValue(sourceDs.Tables[0].Rows[j]["F_Title"].ToString());
dataRow.CreateCell(2).SetCellValue(sourceDs.Tables[0].Rows[j]["F_teaName"].ToString());
dataRow.CreateCell(3).SetCellValue(sourceDs.Tables[0].Rows[j]["F_CreatedDate"].ToString());
dataRow.CreateCell(4).SetCellValue(sourceDs.Tables[0].Rows[j]["F_SeePeopleName"].ToString());
dataRow.CreateCell(5).SetCellValue(sourceDs.Tables[0].Rows[j]["F_SchoolnameRange"].ToString());
dataRow.CreateCell(6).SetCellValue(Convert.ToDateTime(sourceDs.Tables[0].Rows[j]["F_StartTime"]).ToString("yyyy-MM-dd HH:mm") + "~" + Convert.ToDateTime(sourceDs.Tables[0].Rows[j]["F_EndTime"]).ToString("yyyy-MM-dd HH:mm"));
dataRow.CreateCell(7).SetCellValue(sourceDs.Tables[0].Rows[j]["taskfalg"].ToString());
//}
rowIndex++;
}
}
workbook.Write(ms);
ms.Flush();
ms.Position = 0;
workbook = null;
return ms;
}