操作EXCEL(1)

读取表内容到以EX CEL形式发送到客户端

 using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

using System.IO;
using System.Text;
namespace ZilongComExcel
{
 /// <summary>
 /// ComExcel 的摘要说明。
 /// </summary>
 public class ComExcel:System.Web.UI.Page
 {
  public ComExcel()
  {
   //
   // TODO: 在此处添加构造函数逻辑
   //
  }
  public void ExportToExcel(Control ctl, string fileName, string encoding)
  {
   HttpContext context = HttpContext.Current;
   HttpResponse response = context.Response;
   response.Clear();
   response.Buffer = true;
   response.Charset = encoding;
   response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(fileName) + ".xls");
   response.ContentEncoding = Encoding.GetEncoding(encoding);
   response.ContentType = "application/ms-excel";
   if (ctl.Page != null)
   {
    ctl.Page.EnableViewState = false;
    StringWriter sw = new StringWriter();
    HtmlTextWriter htw = new HtmlTextWriter(sw);
    this.ClearControls(ctl);
    ctl.RenderControl(htw);
    response.Write(sw.ToString());
    response.End();
    ctl.Page.EnableViewState = true;
   }
   else
   {
    StringWriter sw = new StringWriter();
    HtmlTextWriter htw = new HtmlTextWriter(sw);
    ctl.RenderControl(htw);
    response.Write(sw.ToString());
    response.End();
   }

  }

  public void ExportToExcel(Control ctl, string fileName)
  {
   //this.ExportToExcel( ctl,fileName,"utf-8");
   this.ExportToExcel(ctl, fileName, "gb2312");
  }
  public void ExportToExcel(DataTable dt, string fileName)
  {
   DataGrid dg = new DataGrid();
   dg.DataSource = dt.DefaultView;
   dg.AllowPaging = false;
   dg.DataBind();
   this.ExportToExcel(dg, fileName);
  }
  public void ExportToExcel(DataSet ds, string fileName, string tableName)
  {
   DataTable dt = null;
   if (tableName == "")
    dt = ds.Tables[0];
   else
    dt = ds.Tables[tableName];
   this.ExportToExcel(dt, fileName);
  }

  public void ExportToExcel(DataSet ds, string fileName)
  {
   this.ExportToExcel(ds, fileName, "");
  }
  private void ClearControls(Control control)
  {
   for (int i=control.Controls.Count -1; i>=0; i--)
   {
    ClearControls(control.Controls[i]);
   }

   if (!(control is TableCell))
   {
    if (control.GetType().GetProperty("SelectedItem") != null)
    {
     LiteralControl literal = new LiteralControl();
     control.Parent.Controls.Add(literal);
     try
     {
      literal.Text = (string)control.GetType().GetProperty("SelectedItem").GetValue(control,null);
     }
     catch
     {
     }
     control.Parent.Controls.Remove(control);
    }
    else
     if (control.GetType().GetProperty("Text") != null)
    {
     LiteralControl literal = new LiteralControl();
     control.Parent.Controls.Add(literal);
     literal.Text = (string)control.GetType().GetProperty("Text").GetValue(control,null);
     control.Parent.Controls.Remove(control);
    }
   }
   return;
  }
 }
}

1.将工作表Sheet1复制到Sheet2,并将Sheet2更名为“职工工资表”; 2.在“职工工资表”中用公式求出每人的应发数和实发数(应发数=固定工资+浮动工资+各种津贴,实发数=应发数-扣除); 3.将“职工工资表”中各职工姓名“加粗,居中”,并以“实发数”降序排列表格内容; 4.根据“职工工资表”的“姓名”和“实发数”创建一“簇状柱形图”,显示在区域A13:H22,要求以“实发数示意图”为图表标题。 帮助: 1.选择Sheet1中A1:G8间的全部内容,再单击“编辑”→“复制”,再将光标移到Sheet2的A1单元格,单击“编辑”→“粘贴”,再用鼠标双击工作表标签Sheet2,并在上面输入“职工工资表”,然后按回车键即可。 2.选择“职工工资表”中的E3单元格,并输入“=B3+C3+D3”并按回车键,再将光标移到E3单元格右下角的“句柄”,然后按住鼠标左键拖到E8,再在G3单元格中输入“=E3-F3”并按回车键,再将光标移到G3单元格右下角的“句柄”,然后按住鼠标左键拖到G8即可。 3.选择“职工工资表”中A3:A8单元格,单击“格式”→“单元格”,在“字体”选项卡的“字形”栏中选择“加粗”,在“对齐”选项卡的“水平对齐”栏中选择“居中”,再将光标移到其他数据区域内,单击“数据”→“排序”,在弹出窗口的“主要关键字”中选择“实发数”,并选择排列顺序为“递减”,单击“确定”。 4.在“职工工资表”中选择“姓名”和“实发数”数据,即A2:A8及G2:G8,单击“插入”→“图表”,在“图表类型”中选择“柱形图”,在“子图表类型”中选择“簇状柱形图”,单击“完成”,并在选中图表的情况下单击“图表”→“图表选项”,在“标题”选项卡的“图表标题”栏中输入“实发数示意图”,单击“确定”,再将图表通过拖动和缩放,显示在区域A13:H22。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值