skl

  IT博客 :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理 ::
  18 随笔 :: 1 文章 :: 7 评论 :: 0 Trackbacks

2009年1月5日 #

     摘要: .net 弹出框  阅读全文
posted @ 2009-01-05 11:03 单 阅读(485) | 评论 (0)编辑 收藏

2008年12月30日 #



alter table 表名 drop colum 列名
则将该表中这一列删除包括该列的关系
posted @ 2008-12-30 17:26 单 阅读(1272) | 评论 (2)编辑 收藏

2008年12月29日 #

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>如何让select的样式也漂亮起来</title>
<style type="text/css">
<!--
.select * {
  margin: 0;
  padding: 0;
}
.select {
  border:1px solid #cccccc;
  float: left;
  display: inline;
  }
.select div {
  border:1px solid #f9f9f9;
  float: left;
}
/* 子选择器,在FF等非IE浏览器中识别 */
.select>div {
  width:120px;
  height: 17px;
  overflow:hidden;
  }
 
/* 通配选择符,只在IE浏览器中识别 */
* html .select div select {
  display:block;
  float: left;
  margin: -2px;
}
.select div>select {
  display:block;
  width:124px;
  float:none;
  margin: -2px;
  padding: 0px;
}
.select:hover {
  border:1px solid #666666; //鼠标移上的效果
}
.select select>option {
  text-indent: 2px; //option在FF等非IE浏览器缩进2px
}
-->
</style>
</head>
<body><div class="select">
 <div>
 <select>
  <option>看见效果了吧</option>
  <option>看见效果了吧</option>
  <option>看见效果了吧</option>
 </select>
 </div>
</div>
</body>
</html>
posted @ 2008-12-29 12:03 单 阅读(7662) | 评论 (5)编辑 收藏

<style>
select{
width:120;
margin:-2px;
}
.myselect{
width:118;
border:1px solid #000099;
overflow:hidden
}
</style>


<span class=myselect>
<select>
    <option>1</option>
    <option>2</option>
</select>
</span>

posted @ 2008-12-29 12:03 单 阅读(665) | 评论 (0)编辑 收藏

2008年12月26日 #

<style type="text/css">

A IMG {
              FILTER: gray
            }
A:hover IMG {FILTER:
}</style><a href="http://www.sincol.com">
<img
border="0" alt="" src="http://www.sincol.net/uploads/img/lanrentuku_colorpic.jpg" /> </a>

posted @ 2008-12-26 16:02 单 阅读(431) | 评论 (0)编辑 收藏

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>火红五月天</title>
<style type="text/css">
#menu ul li{
float:left;
list-style:none;
margin:1px;
}
a{
background:url(http://www.anace.cn/images/zdbs.gif) no-repeat 0px 5px;  /* 这里是背景的关键部分 */
text-decoration:none;
color:#CCCCCC;
font-size:100%;
background-color:#235F9A;
padding:2px 4px;
border-top:2px solid #666;
border-left:2px solid #666;
border-bottom:1px solid #999;
border-right:1px solid #999;}
a:hover{
background-color:#3276A9;
background:url(http://www.anace.cn/images/rss_article.gif) no-repeat 0px 5px;  /* 这里是背景的关键部分 */
border-bottom:2px solid #666;
border-right:2px solid #666;
border-top:1px solid #999;
border-left:1px solid #999;}
</style>
</head>
<body>
<div id="menu">
<ul>
<li><a href="#">首页</a></li><li><a href="#">音乐</a></li><li><a href="#">分享</a></li><li><a href="#">有趣</a></li><li><a href="#">对联</a></li>
</ul>
</div>
</body>
</html>
posted @ 2008-12-26 15:55 单 阅读(239) | 评论 (0)编辑 收藏

truncate,delete,drop的比较.


注意:这里说的delete是指不带where子句的delete语句


相同点:truncate和不带where子句的delete, 以及drop都会删除表内的数据


不同点:
1. truncate和 delete只删除数据不删除表的结构(定义)
drop语句将删除表的结构被依赖的约束(constrain),触发器(trigger),索引(index); 依赖于该表的存储过程/函数将保留,但是变为invalid状态.


2.delete语句是DML,这个操作会放到rollback segement中,事务提交之后才生效;如果有相应的trigger,执行的时候将被触发.
truncate,drop是DDL, 操作立即生效,原数据不放到rollback segment中,不能回滚. 操作不触发trigger.


3.delete语句不影响表所占用的extent, 高水线(high watermark)保持原位置不动
显然drop语句将表所占用的空间全部释放
truncate 语句缺省情况下见空间释放到 minextents个 extent,除非使用reuse storage; truncate会将高水线复位(回到最开始).


4.速度,一般来说: drop> truncate > delete


5.安全性:小心使用drop 和truncate,尤其没有备份的时候.否则哭都来不及
使用上,想删除部分数据行用delete,注意带上where子句. 回滚段要足够大.
想删除表,当然用drop
想保留表而将所有数据删除. 如果和事务无关,用truncate即可. 如果和事务有关,或者想触发trigger,还是用delete.
如果是整理表内部的碎片,可以用truncate跟上reuse stroage,再重新导入/插入数据

posted @ 2008-12-26 10:49 单 阅读(175) | 评论 (0)编辑 收藏

truncate是oracle中独有的关键字吧!它的作用是清空一个表格,在删除数据方面,其与delete有一些区别:

1、在功能上,truncate是清空一个表的内容,它相当于delete from table_name。
2、delete是dml操作,truncate是ddl操作;因此,用delete删除整个表的数据时,会产生大量的roolback,占用很多的rollback segments, 而truncate不会。
3、在内存中,用delete删除数据,表空间中其被删除数据的表占用的空间还在,便于以后的使用,另外它是“假相”的删除,相当于windows中用delete删除数据是把数据放到回收站中,还可以恢复,当然如果这个时候重新启动系统(OS或者RDBMS),它也就不能恢复了!
而用truncate清除数据,内存中表空间中其被删除数据的表占用的空间会被立即释放,相当于windows中用shift+delete删除数据,不能够恢复!
4、truncate 调整high water mark 而delete不;truncate之后,TABLE的HWM退回到 INITIAL和NEXT的位置(默认)delete 则不可以。
5、truncate 只能对TABLE,delete 可以是table,view,synonym。
6、TRUNCATE TABLE 的对象必须是本模式下的,或者有drop any table的权限 而 DELETE 则是对象必须是本模式下的,或被授予 DELETE ON SCHEMA.TABLE 或DELETE ANY TABLE的权限。
7、在外层中,truncate或者delete后,其占用的空间都将释放。
8、truncate和delete只删除数据,而drop则删除整个表(结构和数据)。
posted @ 2008-12-26 10:15 单 阅读(201) | 评论 (0)编辑 收藏

2008年12月25日 #

最近一个项目用到excel的导入和导出,遇到了这个问题,

把数据导出为excel文件后,修改部分数据,然后再导入该excel文件时就会

提示“不是预期的格式错误”。经过详细的排查,发现导出后的excel文件格式

不是真正的 Microsoft Office Excel 的文件格式,而是html的格式的excel文件,

大家可以通过 用记事本 打开该excel文件来验证,

如果打开是乱码,说明是Microsoft Office Excel 的文件格式,此格式文件导入是没有问题的。

如果打开是不是乱码,你就会发现是一些 html格式的代码,此时的excel文件在导入的时候就会报上边的错误。

然后再继续检查原因,发现如果用的是通过HttpContext.Current.Response的方式,改变ContentType的类型来输出的excel,那么导出的excel文件的格式就是html类型的excel文件。

如果采用excel组件的方式来导出excel文件,那么导出的excel文件的格式就是正常的可以导入的格式。

到此问题基本解决,希望对遇到同样问题的朋友有所帮助!

另:如果把导出的html格式的excel文件打开,另存为 正确的excel文件格式的话,也是可以正常导入的。

posted @ 2008-12-25 17:03 单 阅读(438) | 评论 (0)编辑 收藏

2008年12月19日 #

 
 
一、asp.net中导出Execl的方法:

在asp.net中导出Execl有两种方法,一种是将导出的文件存放在服务器某个文件夹下面,然后将文件地址输出在浏览器上;一种是将文件直接将文件输出流写给浏览器。在Response输出时,t分隔的数据,导出execl时,等价于分列,n等价于换行。
1、将整个html全部输出execl

此法将html中所有的内容,如按钮,表格,图片等全部输出到Execl中。
   Response.Clear();    
   Response.Buffer=   true;    
   Response.AppendHeader("Content-Disposition","attachment;filename="+DateTime.Now.ToString("yyyyMMdd")+".xls");          
   Response.ContentEncoding=System.Text.Encoding.UTF8;  
   Response.ContentType   =   "application/vnd.ms-excel";  
   this.EnableViewState   =   false;  


这里我们利用了ContentType属性,它默认的属性为text/html,这时将输出为超文本,即我们常见的网页格式到客户端,如果改为ms-excel将将输出excel格式,也就是说以电子表格的格式输出到客户端,这时浏览器将提示你下载保存。ContentType的属性还包括:image/JPEG;text/HTML;image/GIF;vnd.ms-excel/msword 。同理,我们也可以输出(导出)图片、word文档等。下面的方法,也均用了这个属性。

2、将DataGrid控件中的数据导出Execl

上述方法虽然实现了导出的功能,但同时把按钮、分页框等html中的所有输出信息导了进去。而我们一般要导出的是数据,DataGrid控件上的数据。
System.Web.UI.Control ctl=this.DataGrid1;
//DataGrid1是你在窗体中拖放的控件
HttpContext.Current.Response.AppendHeader("Content-Disposition","attachment;filename=Excel.xls");
HttpContext.Current.Response.Charset ="UTF-8";    
HttpContext.Current.Response.ContentEncoding =System.Text.Encoding.Default;
HttpContext.Current.Response.ContentType ="application/ms-excel";
ctl.Page.EnableViewState =false;   
System.IO.StringWriter  tw = new System.IO.StringWriter() ;
System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter (tw);
ctl.RenderControl(hw);
HttpContext.Current.Response.Write(tw.ToString());
HttpContext.Current.Response.End();


如果你的DataGrid用了分页,它导出的是当前页的信息,也就是它导出的是DataGrid中显示的信息。而不是你select语句的全部信息。

为方便使用,写成方法如下:
public void DGToExcel(System.Web.UI.Control ctl)  
  {
   HttpContext.Current.Response.AppendHeader("Content-Disposition","attachment;filename=Excel.xls");
   HttpContext.Current.Response.Charset ="UTF-8";    
   HttpContext.Current.Response.ContentEncoding =System.Text.Encoding.Default;
   HttpContext.Current.Response.ContentType ="application/ms-excel";
   ctl.Page.EnableViewState =false;   
   System.IO.StringWriter  tw = new System.IO.StringWriter() ;
   System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter (tw);
   ctl.RenderControl(hw);
   HttpContext.Current.Response.Write(tw.ToString());
   HttpContext.Current.Response.End();
  }


   用法:DGToExcel(datagrid1);
 
3、将DataSet中的数据导出Execl

有了上边的思路,就是将在导出的信息,输出(Response)客户端,这样就可以导出了。那么把DataSet中的数据导出,也就是把DataSet中的表中的各行信息,以ms-excel的格式Response到http流,这样就OK了。说明:参数ds应为填充有数据表的DataSet,文件名是全名,包括后缀名,如execl2006.xls

public  void CreateExcel(DataSet ds,string FileName)
{
 HttpResponse resp;
 resp = Page.Response;
 resp.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
 resp.AppendHeader("Content-Disposition", "attachment;filename="+FileName);   
 string colHeaders= "", ls_item="";   
 
 //定义表对象与行对象,同时用DataSet对其值进行初始化
 DataTable dt=ds.Tables[0];
 DataRow[] myRow=dt.Select();//可以类似dt.Select("id>10")之形式达到数据筛选目的
        int i=0;
        int cl=dt.Columns.Count;
    
 //取得数据表各列标题,各标题之间以t分割,最后一个列标题后加回车符
 for(i=0;i<cl;i++)
 {
 if(i==(cl-1))//最后一列,加n
 {
 colHeaders +=dt.Columns[i].Caption.ToString() +"n";
 }
 else
 {
 colHeaders+=dt.Columns[i].Caption.ToString()+"t";
 }
      
 }
 resp.Write(colHeaders);
 //向HTTP输出流中写入取得的数据信息
   
 //逐行处理数据  
 foreach(DataRow row in myRow)
 {     
 //当前行数据写入HTTP输出流,并且置空ls_item以便下行数据    
 for(i=0;i<cl;i++)
 {
 if(i==(cl-1))//最后一列,加n
 {
 ls_item +=row[i].ToString()+"n";
 }
 else
 {
 ls_item+=row[i].ToString()+"t";
 }
 
 }
 resp.Write(ls_item);
 ls_item="";
    
 }    
 resp.End();
 }


4、将dataview导出execl
若想实现更加富于变化或者行列不规则的execl导出时,可用本法。
public void OutputExcel(DataView dv,string str)
{
   //dv为要输出到Excel的数据,str为标题名称
   GC.Collect();
   Application excel;// = new Application();
   int rowIndex=4;
   int colIndex=1;
 
   _Workbook xBk;
   _Worksheet xSt;
 
   excel= new ApplicationClass();
 
   xBk = excel.Workbooks.Add(true);
   
   xSt = (_Worksheet)xBk.ActiveSheet;
 
   //
   //取得标题
   //
   foreach(DataColumn col in dv.Table.Columns)
   {
    colIndex++;
    excel.Cells[4,colIndex] = col.ColumnName;
    xSt.get_Range(excel.Cells[4,colIndex],excel.Cells[4,colIndex]).HorizontalAlignment = XlVAlign.xlVAlignCenter;//设置标题格式为居中对齐
   }
 
   //
   //取得表格中的数据
   //
   foreach(DataRowView row in dv)
   {
    rowIndex ++;
    colIndex = 1;
    foreach(DataColumn col in dv.Table.Columns)
    {
     colIndex ++;
     if(col.DataType == System.Type.GetType("System.DateTime"))
     {
      excel.Cells[rowIndex,colIndex] = (Convert.ToDateTime(row[col.ColumnName].ToString())).ToString("yyyy-MM-dd");
      xSt.get_Range(excel.Cells[rowIndex,colIndex],excel.Cells[rowIndex,colIndex]).HorizontalAlignment = XlVAlign.xlVAlignCenter;//设置日期型的字段格式为居中对齐
     }
     else
      if(col.DataType == System.Type.GetType("System.String"))
     {
      excel.Cells[rowIndex,colIndex] = "'"+row[col.ColumnName].ToString();
      xSt.get_Range(excel.Cells[rowIndex,colIndex],excel.Cells[rowIndex,colIndex]).HorizontalAlignment = XlVAlign.xlVAlignCenter;//设置字符型的字段格式为居中对齐
     }
     else
     {
      excel.Cells[rowIndex,colIndex] = row[col.ColumnName].ToString();
     }
    }
   }
   //
   //加载一个合计行
   //
   int rowSum = rowIndex + 1;
   int colSum = 2;
   excel.Cells[rowSum,2] = "合计";
   xSt.get_Range(excel.Cells[rowSum,2],excel.Cells[rowSum,2]).HorizontalAlignment = XlHAlign.xlHAlignCenter;
   //
   //设置选中的部分的颜色
   //
   xSt.get_Range(excel.Cells[rowSum,colSum],excel.Cells[rowSum,colIndex]).Select();
   xSt.get_Range(excel.Cells[rowSum,colSum],excel.Cells[rowSum,colIndex]).Interior.ColorIndex = 19;//设置为浅黄色,共计有56种
   //
   //取得整个报表的标题
   //
   excel.Cells[2,2] = str;
   //
   //设置整个报表的标题格式
   //
   xSt.get_Range(excel.Cells[2,2],excel.Cells[2,2]).Font.Bold = true;
   xSt.get_Range(excel.Cells[2,2],excel.Cells[2,2]).Font.Size = 22;
   //
   //设置报表表格为最适应宽度
   //
   xSt.get_Range(excel.Cells[4,2],excel.Cells[rowSum,colIndex]).Select();
   xSt.get_Range(excel.Cells[4,2],excel.Cells[rowSum,colIndex]).Columns.AutoFit();
   //
   //设置整个报表的标题为跨列居中
   //
   xSt.get_Range(excel.Cells[2,2],excel.Cells[2,colIndex]).Select();
   xSt.get_Range(excel.Cells[2,2],excel.Cells[2,colIndex]).HorizontalAlignment = XlHAlign.xlHAlignCenterAcrossSelection;
   //
   //绘制边框
   //
   xSt.get_Range(excel.Cells[4,2],excel.Cells[rowSum,colIndex]).Borders.LineStyle = 1;
   xSt.get_Range(excel.Cells[4,2],excel.Cells[rowSum,2]).Borders[XlBordersIndex.xlEdgeLeft].Weight = XlBorderWeight.xlThick;//设置左边线加粗
   xSt.get_Range(excel.Cells[4,2],excel.Cells[4,colIndex]).Borders[XlBordersIndex.xlEdgeTop].Weight = XlBorderWeight.xlThick;//设置上边线加粗
   xSt.get_Range(excel.Cells[4,colIndex],excel.Cells[rowSum,colIndex]).Borders[XlBordersIndex.xlEdgeRight].Weight = XlBorderWeight.xlThick;//设置右边线加粗
   xSt.get_Range(excel.Cells[rowSum,2],excel.Cells[rowSum,colIndex]).Borders[XlBordersIndex.xlEdgeBottom].Weight = XlBorderWeight.xlThick;//设置下边线加粗
   //
   //显示效果
   //
   excel.Visible=true;
 
   //xSt.Export(Server.MapPath(".")+""+this.xlfile.Text+".xls",SheetExportActionEnum.ssExportActionNone,Microsoft.Office.Interop.OWC.SheetExportFormat.ssExportHTML);
   xBk.SaveCopyAs(Server.MapPath(".")+""+this.xlfile.Text+".xls");
 
   ds = null;
            xBk.Close(false, null,null);
   
            excel.Quit();
            System.Runtime.InteropServices.Marshal.ReleaseComObject(xBk);
            System.Runtime.InteropServices.Marshal.ReleaseComObject(excel);
    System.Runtime.InteropServices.Marshal.ReleaseComObject(xSt);
            xBk = null;
            excel = null;
   xSt = null;
            GC.Collect();
   string path = Server.MapPath(this.xlfile.Text+".xls");
 
   System.IO.FileInfo file = new System.IO.FileInfo(path);
   Response.Clear();
   Response.Charset="GB2312";
   Response.ContentEncoding=System.Text.Encoding.UTF8;
   // 添加头信息,为"文件下载/另存为"对话框指定默认文件名
   Response.AddHeader("Content-Disposition", "attachment; filename=" + Server.UrlEncode(file.Name));
   // 添加头信息,指定文件大小,让浏览器能够显示下载进度
   Response.AddHeader("Content-Length", file.Length.ToString());
   
   // 指定返回的是一个不能被客户端读取的流,必须被下载
   Response.ContentType = "application/ms-excel";
   
   // 把文件流发送到客户端
   Response.WriteFile(file.FullName);
   // 停止页面的执行
 
   Response.End();
}


 
   上面的方面,均将要导出的execl数据,直接给浏览器输出文件流,下面的方法是首先将其存到服务器的某个文件夹中,然后把文件发送到客户端。这样可以持久的把导出的文件存起来,以便实现其它功能。
5、将execl文件导出到服务器上,再下载。

二、winForm中导出Execl的方法:

方法1:

   SqlConnection conn=new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["conn"]);
   SqlDataAdapter da=new SqlDataAdapter("select * from tb1",conn);
   DataSet ds=new DataSet();
   da.Fill(ds,"table1");
   DataTable dt=ds.Tables["table1"];
   string name=System.Configuration.ConfigurationSettings.AppSettings["downloadurl"].ToString()+DateTime.Today.ToString("yyyyMMdd")+new Random(DateTime.Now.Millisecond).Next(10000).ToString()+".csv";//存放到 web.config中downloadurl指定的路径,文件格式为当前日期+4位随机数
   FileStream fs=new FileStream(name,FileMode.Create,FileAccess.Write);
   StreamWriter sw=new StreamWriter(fs,System.Text.Encoding.GetEncoding("gb2312"));
   sw.WriteLine("自动编号,姓名,年龄");
   foreach(DataRow dr in dt.Rows)
   {
    sw.WriteLine(dr["ID"]+","+dr["vName"]+","+dr["iAge"]);
   }
   sw.Close();
   Response.AddHeader("Content-Disposition", "attachment; filename=" + Server.UrlEncode(name));
   Response.ContentType = "application/ms-excel";// 指定返回的是一个不能被客户端读取的流,必须被下载
   Response.WriteFile(name); // 把文件流发送到客户端
   Response.End();


 
public void Out2Excel(string sTableName,string url)
{
Excel.Application oExcel=new Excel.Application();
Workbooks oBooks;
Workbook oBook;
Sheets oSheets;
Worksheet oSheet;
Range oCells;
string sFile="",sTemplate="";
//
System.Data.DataTable dt=TableOut(sTableName).Tables[0];

sFile=url+"myExcel.xls";
sTemplate=url+"MyTemplate.xls";
//
oExcel.Visible=false;
oExcel.DisplayAlerts=false;
//定义一个新的工作簿
oBooks=oExcel.Workbooks;
oBooks.Open(sTemplate,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing, Type.Missing, Type.Missing);
oBook=oBooks.get_Item(1);
oSheets=oBook.Worksheets;
oSheet=(Worksheet)oSheets.get_Item(1);
//命名该sheet
oSheet.Name="Sheet1";

oCells=oSheet.Cells;
//调用dumpdata过程,将数据导入到Excel中去
DumpData(dt,oCells);
//保存
oSheet.SaveAs(sFile,Excel.XlFileFormat.xlTemplate,Type.Missing,Type.Missing, Type.Missing, Type.Missing, Excel.XlSaveAsAccessMode.xlNoChange, Type.Missing, Type.Missing, Type.Missing);
oBook.Close(false, Type.Missing,Type.Missing);
//退出Excel,并且释放调用的COM资源
oExcel.Quit();

GC.Collect();
KillProcess("Excel");
}

private void KillProcess(string processName)
{
System.Diagnostics.Process myproc= new System.Diagnostics.Process();
//得到所有打开的进程
try
{
foreach (Process thisproc in Process.GetProcessesByName(processName))
{
if(!thisproc.CloseMainWindow())
{
thisproc.Kill();
}
}
}
catch(Exception Exc)
{
throw new Exception("",Exc);
}
}


方法2:
protected void ExportExcel()
  {
   gridbind();
   if(ds1==null) return;
 
   string saveFileName="";
//   bool fileSaved=false;
   SaveFileDialog saveDialog=new SaveFileDialog();
   saveDialog.DefaultExt ="xls";
   saveDialog.Filter="Excel文件|*.xls";
   saveDialog.FileName ="Sheet1";
   saveDialog.ShowDialog();
   saveFileName=saveDialog.FileName;
   if(saveFileName.IndexOf(":")<0) return; //被点了取消
//   excelapp.Workbooks.Open   (App.path & 工程进度表.xls)
 
   Excel.Application xlApp=new Excel.Application();
   object missing=System.Reflection.Missing.Value;
 

   if(xlApp==null)
   {
    MessageBox.Show("无法创建Excel对象,可能您的机子未安装Excel");
    return;
   }
   Excel.Workbooks workbooks=xlApp.Workbooks;
   Excel.Workbook workbook=workbooks.Add(Excel.XlWBATemplate.xlWBATWorksheet);
   Excel.Worksheet worksheet=(Excel.Worksheet)workbook.Worksheets[1];//取得sheet1
   Excel.Range range;
   
 
   string oldCaption=Title_label .Text.Trim ();
   long totalCount=ds1.Tables[0].Rows.Count;
   long rowRead=0;
   float percent=0;
 
   worksheet.Cells[1,1]=Title_label .Text.Trim ();
   //写入字段
   for(int i=0;i<ds1.Tables[0].Columns.Count;i++)
   {
    worksheet.Cells[2,i+1]=ds1.Tables[0].ColumnsIdea [I].ColumnName;
    range=(Excel.Range)worksheet.Cells[2,i+1];
    range.Interior.ColorIndex = 15;
    range.Font.Bold = true;
 
   }
   //写入数值
   Caption .Visible = true;
   for(int r=0;r<ds1.Tables[0].Rows.Count;r++)
   {
    for(int i=0;i<ds1.Tables[0].Columns.Count;i++)
    {
     worksheet.Cells[r+3,i+1]=ds1.Tables[0].Rows[r];    
    }
    rowRead++;
    percent=((float)(100*rowRead))/totalCount;   
    this.Caption.Text= "正在导出数据["+ percent.ToString("0.00")  +"%]...";
    Application.DoEvents();
   }
   worksheet.SaveAs(saveFileName,missing,missing,missing,missing,missing,missing,missing,missing);
   
   this.Caption.Visible= false;
   this.Caption.Text= oldCaption;
 
   range=worksheet.get_Range(worksheet.Cells[2,1],worksheet.Cells[ds1.Tables[0].Rows.Count+2,ds1.Tables[0].Columns.Count]);
   range.BorderAround(Excel.XlLineStyle.xlContinuous,Excel.XlBorderWeight.xlThin,Excel.XlColorIndex.xlColorIndexAutomatic,null);
 
   range.Borders[Excel.XlBordersIndex.xlInsideHorizontal].ColorIndex = Excel.XlColorIndex.xlColorIndexAutomatic;
   range.Borders[Excel.XlBordersIndex.xlInsideHorizontal].LineStyle =Excel.XlLineStyle.xlContinuous;
   range.Borders[Excel.XlBordersIndex.xlInsideHorizontal].Weight =Excel.XlBorderWeight.xlThin;
 
   if(ds1.Tables[0].Columns.Count>1)
   {
    range.Borders[Excel.XlBordersIndex.xlInsideVertical].ColorIndex=Excel.XlColorIndex.xlColorIndexAutomatic;
    }
   workbook.Close(missing,missing,missing);
   xlApp.Quit();
  }


三、附注:
虽然都是实现导出 execl的功能,但在asp.net和winform的程序中,实现的代码是各不相同的。在asp.net中,是在服务器端读取数据,在服务器端把数据以ms-execl的格式,以Response输出到浏览器(客户端);而在winform中,是把数据读到客户端(因为winform运行端就是客户端),然后调用客户端安装的office组件,将读到的数据写在execl的工作簿中。
posted @ 2008-12-19 14:59 单 阅读(296) | 评论 (0)编辑 收藏

仅列出标题  下一页