C# 操作Excel之旁门左道 [ C# | Excel ]

简介:

正文

  一、目的 
 

    从数据库导出数据到Excel中并锁定部分数据不让修改。这里以学生成绩表为例, 学生编号、学生姓名、学生成绩 三个字段从数据库提取,并锁定,老师评价栏在导出后可输入。
 

 

  二、实现

    1.  制作Excel"模板"

      注意这里的模板不是指excel里面的模板,主要为后面导出成html做准备。

      1.1  新建Excel,名称为学生成绩表.xls 。
 

      1.2  设置列名栏目,设置格式字体等信息,最终形式的格式,如图:

 

冻结窗口的方法:比如要冻结第一行,选择第二行的第一个单元格,工具栏->窗口->冻结窗口。

      1.3  锁定区域

        1.3.1  Excel全选->右键 设置单元格格式->保护->去掉 锁定 前复选框

        1.3.2  选择学生编号、学生姓名、学生成绩这三,同上(1.3.1)步骤相反,即勾上 锁定 前的复选框。

        1.3.3  输入测试数据 1 张三 83。

        1.3.4  工具->保护->保护工作表,模板完成!如果你在锁定后再更改前面三列,将出现如下警告框:

 

 

2.  导出Excel为html格式并复制到一个空白的aspx页面中

        2.1  工具栏 文件->另存为网页,导出后的文件为学生成绩表.htm。

        2.2  用记事本或UE打开,可以看到如下部分代码:

< html  xmlns:o ="urn:schemas-microsoft-com:office:office"
xmlns:x
="urn:schemas-microsoft-com:office:excel"
xmlns
="http://www.w3.org/TR/REC-html40" >

< head >
< meta  http-equiv =Content-Type  content ="text/html; charset=gb2312" >
< meta  name =ProgId  content =Excel.Sheet >
< meta  name =Generator  content ="Microsoft Excel 11" >
< link  rel =File-List  href ="学生成绩表.files/filelist.xml" >
< link  rel =Edit-Time-Data  href ="学生成绩表.files/editdata.mso" >
< link  rel =OLE-Object-Data  href ="学生成绩表.files/oledata.mso" >
<!-- [if gte mso 9]><xml>
 <o:DocumentProperties>
  <o:Created>1996-12-17T01:32:42Z</o:Created>
  <o:LastSaved>2009-05-25T06:35:53Z</o:LastSaved>

 

        2.3  新建aspx页面:  Export.aspx。

        2.4  去掉Export.aspx中除<%@ Page 的代码,复制htm里面的代码到空白的Export.aspx中,添加<form id="form1" runat="server">。

    3.  调取数据并显示

      3.1  找到测试数据部分的html代码替换为asp:Repeater控件代码,如下

  <!--
 <tr height=19 style='height:14.25pt'>
  <td height=19 class=xl27 style='height:14.25pt' x:num>1</td>
  <td class=xl27>张三</td>
  <td class=xl27 x:num>83</td>
  <td class=xl25></td>
 </tr>
 
-->
 
< asp:Repeater  ID ="rptData"  runat ="server" >
    
< HeaderTemplate >
    
</ HeaderTemplate >
    
< ItemTemplate >
        
< tr  height =19  style ='height:14.25pt' >
            
< td  height =19  class =xl27  style ='height:14.25pt'  x:num >
                
<% # Eval ( " id " ) %>
            
</ td >
            
< td  class =xl27 >
                
<% # Eval ( " name " ) %>
            
</ td >
            
< td  class =xl27  x:num >
                
<% # Eval ( " achievement " ) %>
            
</ td >
            
< td  class =xl25 ></ td >
        
</ tr >
    
</ ItemTemplate >
    
< FooterTemplate >
    
</ FooterTemplate >
</ asp:Repeater >

      3.2  后台调取数据,导成excel并下载

         这里就不连接数据库了,直接在程序里面模拟一些数据。

     protected   void  Page_Load( object  sender, EventArgs e)
    {
        
this .EnableViewState  =   false ;

        
// 加载数据
        LoadData();

        Response.Clear();
        Response.Buffer 
=   true ;
        Response.Charset 
=   " GB2312 " ;
        Response.AppendHeader(
" Content-Disposition " " attachment; filename= "   +  HttpUtility.UrlEncode( " 学生成绩表.xls " , System.Text.Encoding.UTF8));
        Response.ContentEncoding 
=  System.Text.Encoding.GetEncoding( " GB2312 " );
        Response.ContentType 
=   " application/ms-excel " ;
        
// Response.End();
    }

    
private   void  LoadData()
    {
        IList
< User >  users  =   new  List < User > ();

        
// 测试数据
        users.Add( new  User( 1 " 刘一 " 81 ));
        users.Add(
new  User( 2 " 陈二 " 82 ));
        users.Add(
new  User( 3 " 张三 " 83 ));
        users.Add(
new  User( 4 " 李四 " 84 ));
        users.Add(
new  User( 5 " 王五 " 85 ));
        users.Add(
new  User( 6 " 赵六 " 86 ));
        users.Add(
new  User( 7 " 孙七 " 87 ));
        users.Add(
new  User( 8 " 周八 " 88 ));
        users.Add(
new  User( 9 " 吴九 " 89 ));
        users.Add(
new  User( 10 " 郑十 " 80 ));

        rptData.DataSource 
=  users;
        rptData.DataBind();
    }

    [Serializable]
    
private   class  User
    {

        
public  User()
        {
        }
        
public  User( int  id,  string  name,  decimal  achievement)
        {
            
this ._id  =  id;
            
this ._name  =  name;
            
this ._achievement  =  achievement;
        }
        
private   int  _id;
        
///   <summary>
        
///   编号
        
///   </summary>
         public   int  id
        {
            
get  {  return  _id; }
            
set  { _id  =  value; }
        }
        
private   string  _name;
        
///   <summary>
        
///  姓名
        
///   </summary>
         public   string  name
        {
            
get  {  return  _name; }
            
set  { _name  =  value; }
        }
        
private   decimal  _achievement;
        
///   <summary>
        
///  成绩
        
///   </summary>
         public   decimal  achievement
        {
            
get  {  return  _achievement; }
            
set  { _achievement  =  value; }
        }
    }

       代码说明:

        Page_Load中依次加载数据,然后以ms-excel类型讲web浏览变成excel文件下载。

 

      3.3  导出后的excel截图

        3.3.1  下载

 

 

 3.3.2  修改锁定的三列截图

 

 

很明显,动态输出表格是我们擅长的,也不用你去翻N多N多的API了,最重要的是这里没有Excel进程!



本文转自over140 51CTO博客,原文链接:http://blog.51cto.com/over140/584862,如需转载请自行联系原作者

相关文章
|
SQL 数据挖掘 Python
我去,Excel还有这个操作
我去,Excel还有这个操作
我去,Excel还有这个操作
|
缓存
SXSSF操作excel
SXSSF操作excel
496 0
SXSSF操作excel
|
easyexcel Java
写的不错的一篇Excel操作实现分享给大家(上)
写的不错的一篇Excel操作实现分享给大家
240 0
写的不错的一篇Excel操作实现分享给大家(上)
写的不错的一篇Excel操作实现分享给大家(下)
写的不错的一篇Excel操作实现分享给大家
225 0
|
Web App开发 .NET 测试技术
|
数据格式 Windows