DataTable的數據導出顯示爲報表


  
  #region 將DataTable的數據導出顯示爲報表(不使用Excel對象,使用COM.Excel)

  #region 使用示例
  /*使用示例:
   * DataSet ds=(DataSet)Session["AdBrowseHitDayList"];
   string ExcelFolder=Assistant.GetConfigString("ExcelFolder");
   string FilePath=Server.MapPath(".")+"//"+ExcelFolder+"//";
   
   //生成列的中文對應表
   Hashtable nameList = new Hashtable();
   nameList.Add("ADID", "廣告編碼");
   nameList.Add("ADName", "廣告名稱");
   nameList.Add("year", "年");
   nameList.Add("month", "月");
   nameList.Add("browsum", "顯示數");
   nameList.Add("hitsum", "點擊數");
   nameList.Add("BrowsinglIP", "獨立IP顯示");
   nameList.Add("HitsinglIP", "獨立IP點擊");
   //利用excel對象
   DataToExcel dte=new DataToExcel();
   string filename="";
   try
   {   
    if(ds.Tables[0].Rows.Count>0)
    {
     filename=dte.DataExcel(ds.Tables[0],"標題",FilePath,nameList);
    }
   }
   catch
   {
    //dte.KillExcelProcess();
   }
   
   if(filename!="")
   {
    Response.Redirect(ExcelFolder+"//"+filename,true);
   }
   *
   * */

  #endregion

  /// <summary>
  /// 將DataTable的數據導出顯示爲報表(不使用Excel對象)
  /// </summary>
  /// <param name="dt">數據DataTable</param>
  /// <param name="strTitle">標題</param>
  /// <param name="FilePath">生成文件的路徑</param>
  /// <param name="nameList"></param>
  /// <returns></returns>
  public static string DataExcel(System.Data.DataTable dt, string strTitle, string FilePath, Hashtable nameList)
  {
   COM.Excel.cExcelFile excel = new COM.Excel.cExcelFile();
   ClearFile(FilePath);
   string filename = "短信報警"+DateTime.Now.ToString("yyyyMMddHHmmssff") + ".xls";
   excel.CreateFile(FilePath + filename);
   excel.PrintGridLines = false;

   COM.Excel.cExcelFile.MarginTypes mt1 = COM.Excel.cExcelFile.MarginTypes.xlsTopMargin;
   COM.Excel.cExcelFile.MarginTypes mt2 = COM.Excel.cExcelFile.MarginTypes.xlsLeftMargin;
   COM.Excel.cExcelFile.MarginTypes mt3 = COM.Excel.cExcelFile.MarginTypes.xlsRightMargin;
   COM.Excel.cExcelFile.MarginTypes mt4 = COM.Excel.cExcelFile.MarginTypes.xlsBottomMargin;

   double height = 1.5;
   excel.SetMargin(ref mt1, ref height);
   excel.SetMargin(ref mt2, ref height);
   excel.SetMargin(ref mt3, ref height);
   excel.SetMargin(ref mt4, ref height);

   COM.Excel.cExcelFile.FontFormatting ff = COM.Excel.cExcelFile.FontFormatting.xlsNoFormat;
   string font = "宋體";
   short fontsize = 9;
   excel.SetFont(ref font, ref fontsize, ref ff);

   byte b1 = 1,
    b2 = 22;
   short s3 = 22;
   excel.SetColumnWidth(ref b1, ref b2, ref s3);

   string header = "頁眉";
   string footer = "頁腳";
   excel.SetHeader(ref header);
   excel.SetFooter(ref footer);


   COM.Excel.cExcelFile.ValueTypes vt = COM.Excel.cExcelFile.ValueTypes.xlsText;
   COM.Excel.cExcelFile.CellFont cf = COM.Excel.cExcelFile.CellFont.xlsFont0;
   COM.Excel.cExcelFile.CellAlignment ca = COM.Excel.cExcelFile.CellAlignment.xlsCentreAlign;
   COM.Excel.cExcelFile.CellHiddenLocked chl = COM.Excel.cExcelFile.CellHiddenLocked.xlsNormal;

   // 報表標題
   int cellformat = 1;
   //   int rowindex = 1,colindex = 3;     
   //   object title = (object)strTitle;
   //   excel.WriteValue(ref vt, ref cf, ref ca, ref chl,ref rowindex,ref colindex,ref title,ref cellformat);

   int rowIndex = 1;//起始行
   int colIndex = 0;

 

   //取得列標題    
   foreach (DataColumn colhead in dt.Columns)
   {
    colIndex++;
    string name = colhead.ColumnName.Trim();
    object namestr = (object)name;
    IDictionaryEnumerator Enum = nameList.GetEnumerator();
    while (Enum.MoveNext())
    {
     if (Enum.Key.ToString().Trim() == name)
     {
      namestr = Enum.Value;
     }
    }
    excel.WriteValue(ref vt, ref cf, ref ca, ref chl, ref rowIndex, ref colIndex, ref namestr, ref cellformat);
   }

   //取得表格中的數據   
   foreach (DataRow row in dt.Rows)
   {
    rowIndex++;
    colIndex = 0;
    foreach (DataColumn col in dt.Columns)
    {
     colIndex++;
     if (col.DataType == System.Type.GetType("System.DateTime"))
     {
      object str = (object)(Convert.ToDateTime(row[col.ColumnName].ToString())).ToString("yyyy-MM-dd HH:mm:ss"); ;
      excel.WriteValue(ref vt, ref cf, ref ca, ref chl, ref rowIndex, ref colIndex, ref str, ref cellformat);
     }
     else
     {
      object str = (object)row[col.ColumnName].ToString();
      excel.WriteValue(ref vt, ref cf, ref ca, ref chl, ref rowIndex, ref colIndex, ref str, ref cellformat);
     }
    }
   }
   int ret = excel.CloseFile();

   //   if(ret!=0)
   //   {
   //    //MessageBox.Show(this,"Error!");
   //   }
   //   else
   //   {
   //    //MessageBox.Show(this,"請打開文件c://test.xls!");
   //   }
   return filename;

  }

  #endregion

  #region  清理過時的Excel文件

  private static void ClearFile(string FilePath)
  {
   String[] Files = System.IO.Directory.GetFiles(FilePath);

   if (Files.Length > 10)
   {
    for (int i = 0; i < 10; i++)
    {
     try
     {
      if(Files[i].EndsWith(".xls"))
      {
       System.IO.File.Delete(Files[i]);
      }
     }
     catch(Exception ex)
     {
      string e=ex.ToString();
     }

    }
   }
  }
  #endregion

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章