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;
}

  

最新文章

  1. java中的hashSet和Treeset的分析
  2. addslashes() 函数和stripslashes()函数
  3. 【转】Java魔法堂:String.format详解
  4. java生产者消费者并发协作
  5. Java数据持久层框架 MyBatis之API学习四(xml配置文件详解)
  6. 《收藏》对servlet原理讲解特别详细
  7. java操作数据库的通用的类
  8. jQuery的学习笔记
  9. Python基础——2函数
  10. tree状数据叶子节点与根节点等的递归转换
  11. 项目初始化mysql建库和授权
  12. 只需十四步:从零开始掌握 Python 机器学习(附资源)
  13. kolla-ansible配置cinder 的ceph的多种后端存储池【转】
  14. Deck of Cards ZOJ - 2852 dp 多决策 三维 滚动更新
  15. Python基础2--Python简单数据类型
  16. shell脚本 ------ 输出带颜色的字体
  17. 解决 php提交表单到当前页面,刷新会重复提交 的问题
  18. Week 2
  19. Educational Codeforces Round 13 D. Iterated Linear Function 水题
  20. C语言数据库-二叉树

热门文章

  1. IPC编程之消息队列
  2. Servlet编程实例 续3
  3. MySql中的视图的概念及应用
  4. 《精通Spring4.X企业应用开发实战》读后感第六章(使用外部属性文件)
  5. python包管理
  6. 存储引擎InnoDB
  7. vue,webpack,node间的关系
  8. const define区别
  9. 51nod1138(连续和)
  10. 洛谷P3803 【模板】多项式乘法(FFT)