记——文章分享execl导出功能和自己导出比对

//这个方法是nopi插件
protected void btn_Export_Click(object sender, EventArgs e) { string FileID = ""; if (Request.QueryString["fujian"] != null && Request.QueryString["fujian"].ToString() != "") { FileID = Request.QueryString["fujian"].ToString(); } string title = GetFileTile(FileID);//文件标题 Workbook workbook = new Workbook(); //工作簿 Worksheet sheet = workbook.Worksheets[0]; //工作表 Cells cells = sheet.Cells; //单元格 //列宽 cells.SetColumnWidth(0, 20.00); cells.SetColumnWidth(1, 30.00); cells.SetColumnWidth(2, 30.00); cells.Merge(0, 0, 1, 3);//合并单元格 cells[0, 0].PutValue(title + "/文件已学人员名单"); cells[0, 1].PutValue(""); cells[0, 2].PutValue(""); cells[1, 0].PutValue("序号"); cells[1, 1].PutValue("姓名"); cells[1, 2].PutValue("时间"); string sql = "order by StudyTime"; ds = PublishBLL.GetCommentCount(FileID, sql); if (ds.Tables[0].Rows.Count > 0) { for (int i = 1; i < ds.Tables[0].Rows.Count + 1; i++) { //Aspose.Cells.Style styleTitle = workbook.Styles[workbook.Styles.Add()];//新增样式 //styleTitle.HorizontalAlignment = TextAlignmentType.Center;//文字居中 //styleTitle.Font.Name = "宋体";//文字字体 //styleTitle.Font.Size = 18;//文字大小 //styleTitle.Font.IsBold = true;//粗体 cells[i + 1, 0].PutValue("" + i.ToString().PadLeft(3, '0') + ""); cells[i + 1, 1].PutValue("" + ds.Tables[0].Rows[i - 1]["StudyName"].ToString() + ""); cells[i + 1, 2].PutValue("" + ds.Tables[0].Rows[i - 1]["StudyTime"].ToString() + ""); } } string filename = "统计" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".xls"; string path = ConfigurationManager.AppSettings["StudyFile"] + @"\" + filename + ""; workbook.Save(path);//保存到硬盘 #region 下载 System.IO.MemoryStream ms1 = workbook.SaveToStream();//生成数据流 byte[] bt1 = ms1.ToArray(); string fileName = "统计" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".xls";//客户端保存的文件名 //以字符流的形式下载文件 Response.ContentType = "application/vnd.ms-excel"; //通知浏览器下载文件而不是打开 Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8)); Response.BinaryWrite(bt1); Response.Flush(); Response.End(); #endregion }

 我写的:缺点(无法进行execl格式调整)

 1  public void exeport(string sql, string[] tableHeaders, string FieldList, string fileName)
 2         {
 3             DataTable dataTable = DbSession.Default.FromSql(sql).ToDataTable();
 4             if (dataTable != null && dataTable.Rows.Count > 0)
 5             {
 6                 StringBuilder stringBuilder = new StringBuilder();
 7                 for (int i = 0; i < tableHeaders.Length; i++)
 8                 {
 9                     string arg = tableHeaders[i];
10                     stringBuilder.AppendFormat("\"{0}\"", arg);
11                     stringBuilder.Append("\t");
12                 }
13                 stringBuilder.Remove(stringBuilder.Length - 1, 1);
14                 stringBuilder.Append("\r\n");
15 
16                 string[] array = FieldList.Trim().Split(new char[]
17                 {
18                     ','
19                 });
20                 foreach (DataRow dataRow in dataTable.Rows)
21                 {
22                     for (int j = 0; j < array.Length; j++)
23                     {
24                         stringBuilder.AppendFormat("\"{0}\"", dataRow[array[j]].ToString()); 
25                         stringBuilder.Append("\t");//单元格分割
26                     }
27                     stringBuilder.Remove(stringBuilder.Length - 1, 1);
28                     stringBuilder.Append("\r\n");//换行
29                 }
30                 if (stringBuilder.Length > 0)
31                 {
32                     byte[] bytes = Encoding.Default.GetBytes(stringBuilder.ToString());
33                     FileStream fs = new FileStream(fileName, FileMode.Create, FileAccess.Write);
34                     fs.Write(bytes, 0, bytes.Length);
35                     fs.Flush();
36                     fs.Close();
37                 }
38             }
39 
40         }

 

posted @ 2018-03-05 17:26  无敌大君  阅读(160)  评论(0)    收藏  举报