62,046
社区成员
发帖
与我相关
我的任务
分享
/// <summary>
/// 将数据导出至Excel中
/// </summary>
/// <param name="caption">标题</param>
/// <param name="heading">行标题</param>
/// <param name="dtSource">要导出的DataTable</param>
/// <param name="targetPath">保存的路径(含文件名)</param>
public static void ToExcel(string caption, string[] heading, DataTable dtSource, string targetPath)
{
Excel.Application oXL;
Excel._Workbook oWB;
Excel._Worksheet oSheet;
Excel.Range oRang;
try
{
GC.Collect();
oXL = new Excel.Application();
oXL.Visible = false;
oWB = (Excel._Workbook)(oXL.Workbooks.Add(Missing.Value));
oSheet = (Excel._Worksheet)(oWB.ActiveSheet);
oRang = oSheet.get_Range("A1", oSheet.Cells[1, dtSource.Columns.Count]);
object optional = System.Reflection.Missing.Value;
oRang.Merge(null);
oRang.Value2 = caption;
oRang.Font.Size = "26";
oRang.Font.Bold = true;
oRang.VerticalAlignment = Excel.XlVAlign.xlVAlignBottom;
oRang.HorizontalAlignment = Excel.XlHAlign.xlHAlignCenter;
for (int i = 0; i < heading.Length; i++)
{
oSheet.Cells[2, i + 1] = heading[i];
}
int iRow = 3;
for (int r = 0; r < dtSource.Rows.Count; r++)
{
for (int c = 0; c < dtSource.Columns.Count; c++)
{
oSheet.Cells[iRow, c + 1] = dtSource.Rows[r][c].ToString();
}
iRow++;
}
oSheet.get_Range("A2", "H2").Font.Bold = true;
oSheet.get_Range("A2", "H2").VerticalAlignment = Excel.XlVAlign.xlVAlignCenter;
oSheet.get_Range("A2", "H2").HorizontalAlignment = Excel.XlHAlign.xlHAlignCenter;
oRang = oSheet.get_Range("A2", "H2");
oRang.EntireColumn.AutoFit();
//oRang.EntireColumn.AutoFit();
oXL.Visible = false;
oXL.UserControl = false;
oWB.SaveAs(targetPath, Excel.XlFileFormat.xlWorkbookNormal, null, null, false, false, Excel.XlSaveAsAccessMode.xlShared, false, false, null, null, null);
oWB.Close(null, null, null);
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
/// <summary>
/// 将数据导出至Excel中
/// </summary>
/// <param name="caption">标题</param>
/// <param name="heading">行标题</param>
/// <param name="dtSource">要导出的DataTable</param>
/// <param name="targetPath">保存的路径(含文件名)</param>
public static void ToExcel(string caption, string[] heading, System.Data.DataTable dtSource, string targetPath)
{
Interop.Excel.Application oXL;
Interop.Excel._Workbook oWB;
Interop.Excel._Worksheet oSheet;
Interop.Excel.Range oRang;
try
{
GC.Collect();
oXL = new Interop.Excel.Application();
oXL.Visible = false;
oWB = (Interop.Excel._Workbook)(oXL.Workbooks.Add(Missing.Value));
oSheet = (Interop.Excel._Worksheet)(oWB.ActiveSheet);
oRang = oSheet.get_Range("A1", oSheet.Cells[1, dtSource.Columns.Count]);
object optional = System.Reflection.Missing.Value;
oRang.Merge(null);
oRang.Value2 = caption;
oRang.Font.Size = "26";
oRang.Font.Bold = true;
oRang.VerticalAlignment = Interop.Excel.XlVAlign.xlVAlignBottom;
oRang.HorizontalAlignment = Interop.Excel.XlHAlign.xlHAlignCenter;
oSheet.get_Range("A3", string.Format("F{0}", (dtSource.Rows.Count + 2).ToString().Trim())).NumberFormatLocal = "@";
for (int i = 0; i < heading.Length; i++)
{
oSheet.Cells[2, i + 1] = heading[i];
}
int iRow = 3;
for (int r = 0; r < dtSource.Rows.Count; r++)
{
for (int c = 0; c < dtSource.Columns.Count; c++)
{
oSheet.Cells[iRow, c + 1] = dtSource.Rows[r][c].ToString();
}
iRow++;
}
oSheet.get_Range("A2", "T2").Font.Bold = true;
oSheet.get_Range("A2", "T2").VerticalAlignment = Interop.Excel.XlVAlign.xlVAlignCenter;
oSheet.get_Range("A2", "T2").HorizontalAlignment = Interop.Excel.XlHAlign.xlHAlignCenter;
oRang = oSheet.get_Range("A2", "T2");
oRang.EntireColumn.AutoFit();
oXL.Visible = false;
oXL.UserControl = false;
oWB.SaveAs(targetPath, Interop.Excel.XlFileFormat.xlWorkbookNormal, null, null, false, false, Interop.Excel.XlSaveAsAccessMode.xlExclusive, false, false, null, null, null);
oWB.Close(false, null, null);
oXL.Workbooks.Close();
oXL.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(oXL);
System.Runtime.InteropServices.Marshal.ReleaseComObject(oWB);
oWB = null;
oXL = null;
GC.Collect();
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
private void TableToExcel(System.Data.DataTable tb)
{
string Filename = "aa";
System.Web.HttpContext context = System.Web.HttpContext.Current;
if ((tb != null))
{
context.Response.Clear();
context.Response.Charset = "GB2312";
context.Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
context.Response.ContentType = "application/ms-excel";
context.Response.AppendHeader("content-disposition", "attachment;filename=\"" + System.Web.HttpUtility.UrlEncode(Filename, System.Text.Encoding.GetEncoding("GB2312")) + ".xls\"");
CultureInfo cult = new CultureInfo("zh-CN", true);
StringWriter sw = new StringWriter(cult);
HtmlTextWriter htw = new HtmlTextWriter(sw);
DataGrid dgrid = new DataGrid();
dgrid.DataSource = tb.DefaultView;
dgrid.AllowPaging = false;
dgrid.DataBind();
htw.WriteLine("<meta http-equiv=\"Content-Type\" content=\"text/html;charset=GB2312\">");
dgrid.RenderControl(htw);
context.Response.Write(sw.ToString());
context.Response.End();
}
}
protected void Get_Meal_Voucher_Command(object sender, CommandEventArgs e)
{
if (e.CommandName == "Get_Meal_Voucher")
{
try
{
string caption = string.Format("{0}年{1}月份员工就餐及补贴汇总表", e.CommandArgument.ToString().Substring(0, 4), e.CommandArgument.ToString().Substring(4, 2));
DataTable dtSource = bll.Get_Meal_Voucher(e.CommandArgument.ToString()).Tables[0];
string[] heading = new string[] { "部门", "工号", "姓名", "职务", "购餐种类", "总餐数", "自付金额", "公司补助金额" };
string targetPath = Server.MapPath("~") + "\\temp\\Meal_Voucher.xls";
if (System.IO.File.Exists(targetPath))
{
System.IO.File.Delete(targetPath);
}
CommonFun.ToExcel(caption, heading, dtSource, targetPath);
Response.Redirect(@"~\temp\Meal_Voucher.xls");
}
catch(Exception ex)
{
ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "", "alert('未完成下载, 请重试或联系软件部!');", true);
}
}
}
}