Asp.Net用OWC操作Excel的实例代码

代码如下:

string connstr = System.Configuration.ConfigurationManager.ConnectionStrings["DqpiHrConnectionString"].ToString();
        SqlConnection conn = new SqlConnection(connstr);
        SqlDataAdapter sda = new SqlDataAdapter(sql1.Text, conn);
        DataSet ds = new DataSet();
        conn.Open();
        sda.Fill(ds);
        conn.Close();
        OWC10.SpreadsheetClass xlsheet;
        xlsheet= new OWC10.SpreadsheetClass();
        DataRow dr;
        int i = 0;
        for(int ii=0;ii<ds.Tables[0].Rows.Count;ii++)
        {
            dr = ds.Tables[0].Rows[ii];
            //合并单元格
            xlsheet.get_Range(xlsheet.Cells[i+1, 1], xlsheet.Cells[i+1, 8]).set_MergeCells(true);
            xlsheet.get_Range(xlsheet.Cells[i + 5, 1], xlsheet.Cells[i + 5, 3]).set_MergeCells(true);
            xlsheet.get_Range(xlsheet.Cells[i + 5, 4], xlsheet.Cells[i + 5, 6]).set_MergeCells(true);
            xlsheet.get_Range(xlsheet.Cells[i + 5, 7], xlsheet.Cells[i + 5, 8]).set_MergeCells(true);
            xlsheet.ActiveSheet.Cells[i + 1, 1] = dr["姓名"].ToString() + "自然情况";
            //字体加粗
            xlsheet.get_Range(xlsheet.Cells[i + 1, 1], xlsheet.Cells[i + 1, 14]).Font.set_Bold(true);
            //单元格文本水平居中对齐
            xlsheet.get_Range(xlsheet.Cells[i + 1, 1], xlsheet.Cells[i + 1, 14]).set_HorizontalAlignment(OWC10.XlHAlign.xlHAlignCenter);
            //设置字体大小
            xlsheet.get_Range(xlsheet.Cells[i + 1, 1], xlsheet.Cells[i + 1, 14]).Font.set_Size(14);
            //设置列宽
            xlsheet.get_Range(xlsheet.Cells[i + 1, 8], xlsheet.Cells[i + 1, 8]).set_ColumnWidth(20);
            //画边框线
            xlsheet.get_Range(xlsheet.Cells[i + 1, 1], xlsheet.Cells[i+5, 8]).Borders.set_LineStyle(OWC10.XlLineStyle.xlContinuous);
            //写入数据  (这里由DS生成)
            xlsheet.ActiveSheet.Cells[i + 2, 1] = "姓名";
            xlsheet.ActiveSheet.Cells[i + 2, 2] = dr["姓名"].ToString();
            xlsheet.ActiveSheet.Cells[i + 2, 3] = "曾用名";
            xlsheet.ActiveSheet.Cells[i + 2, 4] = dr["曾用名"].ToString();
            xlsheet.ActiveSheet.Cells[i + 2, 5] = "出生年月";
            xlsheet.ActiveSheet.Cells[i + 2, 6] = DateTime.Parse(dr["出生年月"].ToString()).Year.ToString() + "-" + DateTime.Parse(dr["出生年月"].ToString()).Month.ToString();
            xlsheet.ActiveSheet.Cells[i + 2, 7] = " 参加工作时间";
            xlsheet.ActiveSheet.Cells[i + 2, 8] = DateTime.Parse(dr["参加工作时间"].ToString()).Year.ToString() + "-" + DateTime.Parse(dr["参加工作时间"].ToString()).Month.ToString();
            xlsheet.ActiveSheet.Cells[i + 3, 1] = "性别";
            xlsheet.ActiveSheet.Cells[i + 3, 2] = dr["性别"].ToString();
            xlsheet.ActiveSheet.Cells[i + 3, 3] = "民族";
            xlsheet.ActiveSheet.Cells[i + 3, 4] = dr["民族"].ToString();
            xlsheet.ActiveSheet.Cells[i + 3, 5] = "政治面貌";
            xlsheet.ActiveSheet.Cells[i + 3, 6] = dr["政治面貌"].ToString();
            xlsheet.ActiveSheet.Cells[i + 3, 7] = "职称";
            xlsheet.ActiveSheet.Cells[i + 3, 8] = dr["职称"].ToString();
            xlsheet.ActiveSheet.Cells[i + 4, 1] = "学历";
            xlsheet.ActiveSheet.Cells[i + 4, 2] = dr["学历"].ToString();
            xlsheet.ActiveSheet.Cells[i + 4, 3] = "学位";
            xlsheet.ActiveSheet.Cells[i + 4, 4] = dr["学位"].ToString();
            xlsheet.ActiveSheet.Cells[i + 4, 5] = "职务";
            xlsheet.ActiveSheet.Cells[i + 4, 6] = dr["职务"].ToString();
            xlsheet.ActiveSheet.Cells[i + 4, 7] = "档案号码";
            //Excel不支持0开头输入,加上姓氏首字母正好是编号全称
            xlsheet.ActiveSheet.Cells[i + 4, 8] = dr["姓氏首字母"].ToString() + dr["档案号码"].ToString();
            xlsheet.ActiveSheet.Cells[i + 5, 1] = "现从事专业:" + dr["现从事专业"].ToString();
            xlsheet.ActiveSheet.Cells[i + 5, 4] = "工作单位:" + dr["工作单位"].ToString();
            xlsheet.ActiveSheet.Cells[i + 5, 7] = "身份证:" + dr["身份证号"].ToString();
            i += 6;
        }
        try
        {
            string D = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() +
            DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString()+
            DateTime.Now.Millisecond.ToString();
            xlsheet.Export(Server.MapPath("./")+"\\"+D+".xls", OWC10.SheetExportActionEnum.ssExportActionNone, OWC10.SheetExportFormat.ssExportXMLSpreadsheet);
            Response.Write("<script>window.open('"+D+".xls')</script>");
        }
        catch
        {
        }
    }

(0)

相关推荐

  • ASP.NET批量操作基于原生html标签的无序列表的三种方法

    在网页开发中,经常要用到无序列表.事实上在符合W3C标准的div+css布局中,无序列表被大量使用,ASP.NET虽然内置了BulletedList控件,用于创建和操作无序列表,但感觉不太好用.本篇介绍服务器端ASP.NET批量操作基于原生html标签的无序列表的三种方法. 方法一,将li元素做成html控件,加上id,用FindControl方法. aspx代码: <form id="form1" runat="server"> <ul>

  • asp.net操作xml增删改示例分享

    复制代码 代码如下: 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.HtmlCo

  • asp.net操作javascript:confirm返回值的两种方式

    在asp.net中使用confirm可以分为两种: 1.没有使用ajax,confirm会引起也面刷新 2.使用了ajax,不会刷新 A.没有使用ajax,可以用StringBuilder来完成. (一)asp.net用StringBuilder控制后台操作javascript:confirm返回值,此方法比较烦琐 1.后台启动事件 StringBuilder sb = new StringBuilder(); sb.Append("<script language='javascript

  • asp.net 操作cookie的简单实例

    复制代码 代码如下: protected void Page_Load(object sender, EventArgs e)    {        if (!IsPostBack)        {            HttpCookie cookie = Request.Cookies["loginInfo"]; if (cookie != null)            {                cb_repwd.Checked = true;          

  • ASP.NET中操作SQL数据库(连接字符串的配置及获取)

    在WebConfig中配置数据库连接字符串,代码如下: 复制代码 代码如下: <connectionStrings> <add name="ConnectionString" connectionString="user id=用户名;password=密码;initial catalog=数据库名称;data source=服务器名称"/> </connectionStrings> 然后在Webform_1.aspx.cs里面获

  • asp.net中Repeart选中整行操作实例

    本文实例讲述了asp.net中Repeart选中整行操作的方法.分享给大家供大家参考. 具体实现方法如下: 复制代码 代码如下: <asp:Repeater runat="server" ID="rpt_Student">             <HeaderTemplate>                  <table>                     <tr  id="tr_<%#this.

  • ASP.net中获取客户端参数操作系统信息

    复制代码 代码如下: /// <summary> /// 获取用户操作系统信息 /// </summary> /// <returns></returns> public string GetUserOS() { string strSysVersion = "其他"; HttpRequest Request = HttpContext.Current.Request; string strAgentInfo = Request.Serv

  • Asp.Net中Cache操作类实例详解

    本文以一个Asp.Net的Cache操作类实例代码来详细描述了cache缓存的结构及实现方法,完整代码如下所示: /// <head> /// <function> /// 存储类(存储UserInfo信息) /// </function> /// <description> /// 用Cache存储用户信息 /// 在指定间隔(TimeOut)内取,则可以从Cache中取, /// 如果超出存储时间,则从数据库取用户信息数据 /// 作為所有用户信息的存儲

  • asp.net操作ini文件示例

    复制代码 代码如下: using System;using System.Data;using System.Configuration;using System.Web;using System.Web.Security;using System.Web.UI;using System.Web.UI.WebControls;using System.Web.UI.WebControls.WebParts;using System.Web.UI.HtmlControls; using Syste

  • ASP.NET操作各类时间段获取方法汇总

    本文实例讲述了ASP.NET操作各类时间段获取方法,分享给大家供大家参考.具体如下: 复制代码 代码如下: DateTime dt = DateTime.Now; //当前时间 DateTime startWeek = dt.AddDays(1- Convert.ToInt32(dt.DayOfWeek.ToString("d"))); //本周周一 DateTime endWeek = startWeek.AddDays(6); //本周周日 DateTime startMonth

  • 一个ASP.NET的MYSQL的数据库操作类自己封装的

    /** * 类说明:对MYSQL数据库的操作类 */ using System; using System.Data; using MySql.Data.MySqlClient; namespace Niunan.BYLW.Utility { /// <summary>对MYSQL数据库的操作类 /// /// </summary> public class MYSQLHelper { private MySqlConnection conn = null; private MyS

随机推荐