27 Apr 2004

打开一个Excel模板文件填充数据另存为一个文件

最近做的东西要求打开一个Excel文件模板后填充数据
开始用OleDb连接该Excel文件更新老是出错误 “需要一个可更新的.....“
http://expert.csdn.net/Expert/topic/2992/2992809.xml?temp=.4825403
如果是access这个错误多半是权限引起的 可是我做的是winform 权限是admin啊
未果
后来用ODBC测试了几个字段 都成功了
开始写程序 后发现就有那么2个单元格不听话 提示参数错误
郁闷
http://expert.csdn.net/Expert/topic/3000/3000113.xml?temp=.3331262
仍未果
没办法翻google
获得
http://www.c-sharpcorner.com/winforms/ExcelReadMG.asp
用com组件,可行哦,今早上csdn就有网友pm我问是如何搞定的
那就写到这里,
以下是部分代码,使用前要先按上文中的说明添加Com引用
...........
string strFileName = Environment.CurrentDirectory+@"\template\template.tpl";
    string strSaveFileName =
Environment.CurrentDirectory+@"\excel\"+System.DateTime.Now.ToString().Replace(":","").Replace("-","").Replace(" ","")+@".xls";
    Excel.Application ThisApplication = new Excel.ApplicationClass();
    Excel.Workbook ThisWorkBook;
    object missing = System.Reflection.Missing.Value;
    try
    { 
     //加载Excel模板文件
     ThisWorkBook = ThisApplication.Workbooks.Open(strFileName,missing,missing,missing,missing,missing,missing,missing,missing,missing,missing,missing,missing);
     Excel.Worksheet ThisSheet = (Excel.Worksheet)ThisWorkBook.Sheets[1];
     ThisSheet.Cells[7,1] = dtEnd.ToShortDateString();
     progressBar.Value = 30;
     ThisApplication.Visible = false;
     DataRow dr = dt.Rows[0];
     for( int i=0; i     {
      ThisSheet.Cells[7,i+2] = dr[i].ToString();
      progressBar.Value +=1;
     }
     //更新数据后另存为新文件
     ThisSheet.SaveAs(strSaveFileName,missing,missing,missing,missing,missing,missing,missing,missing);
    }
    catch{}
    finally
    {
     ThisApplication.Quit();
     ThisWorkBook = null;
     ThisApplication = null;
     //dt = null;
    }
    try
    { //打开刚才生成的Excel文件
     Excel.Workbook NewWorkBook;
     NewWorkBook = NewApplication.Workbooks.Open(strSaveFileName,missing,missing,missing,missing,missing,missing,missing,missing,missing,missing,missing,missing);
     Excel.Worksheet NewSheet = (Excel.Worksheet)NewWorkBook.Sheets[1];
     NewApplication.Visible = true;

//也可以使用System.Diagnostics.Process.Start(strSaveFileName);来打开新文件

    }

打开Excel后进程内会多出一个excel进程
需要手工清理
参考这片文章

http://www.eggheadcafe.com/articles/20021012.asp

http://www.github.com/476/archive.aspx

05 Apr 2004

DataGrid点击删除按钮弹出确认对话框一个好办法

原文地址
http://expert.csdn.net/Expert/topic/2852/2852126.xml?temp=.4770471 
其中
wf5360308(峰)
提供的方法
datagrid-》属性生成器-》列-》添加按钮列-》删除-》文本(T)->在文本框里加上:
<div id="de" onclick="JavaScript:return confirm('确定删除吗?')">删除</div>

不错的办法哦~

05 Apr 2004

如何在ItemDataBound内获得DataGrid的列名称HeaderText

如果 AutoGenerateColumns=“false” 使用绑定列,模板列 则比较好办,直接使用 YourDataGrid.Columns[编号].HeaderText 获得 可是,如果 AutoGenerateColumns=“true” ,就比较麻烦了,因为这时YourDataGrid.Columns.Count是0 但是可以变通做到。

<%@ Page Language="C#" Debug="true" %>
<%@ Import Namespace="System.Data" %>
<!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" xml:lang="gb2312" lang="gb2312">
<head>
<title> ItemDataBoundGetColumnName </title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<meta name="title" content="" />
<meta name="author" content="活靶子,Huobazi,www.github.com" />
<meta name="subject" content="" />
<meta name="language" content="gb2312" />
<meta name="keywords" content="" />
<meta name="Copyright" content="www.github.com" />
<meta name="robots" content="all" />
<script language="c#" runat="server">
void BindGrid()
{
        DataTable dt = new DataTable();
        DataRow dr;
        dt.Columns.Add(new DataColumn("IntegerValue", typeof(Int32)));
        dt.Columns.Add(new DataColumn("StringValue", typeof(string)));
        dt.Columns.Add(new DataColumn("DateTimeValue", typeof(DateTime)));
        dt.Columns.Add(new DataColumn("BoolValue", typeof(bool)));
        dt.Columns.Add(new DataColumn("CurrencyValue", typeof(double)));
        for (int i = 0; i < 36; i++) {
            dr = dt.NewRow();
            dr[0] = i;
            dr[1] = "项 " + i.ToString();
            dr[2] = DateTime.Now;
            dr[3] = (i % 2 != 0) ? true : false;
            dr[4] = 1.23 * (i+1);
            dt.Rows.Add(dr);
        }
  dg.DataSource= dt;
  dg.DataBind();
}
void Page_Load(object o, EventArgs e)
{
     dg.PagerStyle.Mode = PagerMode.NumericPages;
     if(!IsPostBack)
     {
        BindGrid();
     }
}
 
void btnClick(object o , EventArgs e)
{
     Response.Write("页面回发,但是不执行ItemDataBound");
}
 
void PageChange(object o , DataGridPageChangedEventArgs e)
{
     dg.CurrentPageIndex = e.NewPageIndex;
     BindGrid();
}
void ItemDataBound(object o , DataGridItemEventArgs e)
{
     if(e.Item.ItemIndex > -1 )//必须
     {
          DataRowView drv = (DataRowView)e.Item.DataItem;
          for (int i=0; i<drv.Row.Table.Columns.Count ; i++)
          {  
            Response.Write(drv.Row.Table.Columns[i].ColumnName + "  ");
          }
     }
}
</script>
 
</head>
<body>
<form runat="server">
  <ASP:DataGrid id="dg" runat="server"       
   HeaderStyle-BackColor="#aaaadd"
   AutoGenerateColumns="true"
   AllowPaging="true"
   PageSize="6"
    OnItemDataBound="ItemDataBound"
    OnPageIndexChanged="PageChange"
  >
  </asp:DataGrid>
  </br>
   <asp:button id="btn" runat="server" onclick="btnClick" Text="我是按钮,按我一下"></asp:button>
   <p>在AutoGenerateColumns="false"使用绑定列或者模板列的时候只需要使用 </P>
   <p><font color="blut">YourDataGrid.Columns[编号].HeaderText</font> 去获取
  </P>
</form>
</body>
</html>
 

23 Feb 2004

用Asp.Net抓取web页面

演示:
http://www.github.com/demo/286.aspx


代码高亮显示(HightLight in Source Code Viewer)


http://www.github.com/code/default.aspx/S7s2zPJbeI9PJA/OK
HpAVwVaUEhf02EgEOgeB3Yd5Z7fLL3ZNgdv7XIqertUOYm9kbQ62
LshbJBwvWPBBYa4EyVMhvMhyEDPH8chU5ornn8=.Aspx

<html>
<head>
 

<SCRIPT runat="server">
  void Page_Load(Object sender, EventArgs e) {

    WebRequest req = WebRequest.Create("http://www.im286.com/index.php");
 string urliii="src="; //图片使用绝对连接
 urliii+="http://www.im286.com/";
    try {
        WebResponse result = req.GetResponse();
        Stream ReceiveStream = result.GetResponseStream();

        Byte[] read = new Byte[512];
        int bytes = ReceiveStream.Read(read, 0, 512);

        lblHTML.Text = "";
        while (bytes > 0)
        {

          // 注意:
          // 下面假定响应使用 gb2312 作为编码方式。
          // 如果内容以 ANSI 代码页形式(例如,932)发送,则使用类似下面的语句:
          //Encoding encode = System.Text.Encoding.GetEncoding("shift-jis");
          Encoding encode = System.Text.Encoding.GetEncoding("gb2312");
          lblHTML.Text = lblHTML.Text + encode.GetString(read, 0, bytes);
   
          bytes = ReceiveStream.Read(read, 0, 512);
   
        }
    } catch(Exception) {
        lblHTML.Text = "检索页时出错";
    }
 lblHTML.Text = lblHTML.Text.Replace("src=",""+urliii+"");
 lblHTML.Text = lblHTML.Text.Replace("\"","");
  }
</SCRIPT>
</head>

<body>
  <form method="post" action="Webrequest.aspx" runat="server">
   
    <asp:Label runat=server ID="lblHTML" Rows="30" Cols="80" EnableViewState="false"  Wrap="True"></asp:Label>
  </form>

</body>
</html>

<%@ Page language="C#" Trace="True" %>
<%@ Import Namespace="System.Net" %>
<%@ Import Namespace="System.IO" %>

04 Feb 2004

域名Whois信息查询(ASP.NET+C#)

去年写的了,是从点缀的博客学来的
看演示请点这里:http://www.github.com/whois.aspx
代码如下:
<% @Page Language="C#" %>
<% @Import Namespace="System.Net.Sockets" %>
<% @Import Namespace="System.Text" %>
<% @Import Namespace="System.IO" %>
<html>
<head>
<title>.Com/.Net/.Org/.Cn 域名Whois信息查询</title>
<meta name="keywords" content=".Com,.Net,.Org,.Cn 域名Whois信息查询">
<meta name="generator" content=".Com/.Net/.Org,.Cn 域名Whois信息查询">
<meta name="description" content=".Com/.Net/.Org,.Cn 域名Whois信息查询">
<style>
<!--
body,input{
        font-family: Tahoma, Verdana; color: #004080; font-size: 12px        
        }
a:link,a:visited{
        text-decoration: none; color: #004080
        }
-->
</style>
</head>
<body>
<form id="fmQuery" runat="server">
&nbsp;要查询的域名域名:
www.<asp:TextBox id="txtDomain" width="100" value="ASPXBOY.COM" runat="server" />
&nbsp; <asp:Button id="btnQuery" OnClick="btn_click"
text="查询!" runat="server" />(只能查询.Com/.Net/.Org/.Cn 域名Whois的信息)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href ="WhoisCode.htm" title="View the source code here!">源代码在这里</a>
<BR><HR width="550" height="1" align="left"><BR>
<asp:label id="lblResult" runat="server" />
</form>
</body>
</html>
<script language="C#" runat="server">
void btn_click(Object sender, EventArgs e)
{
String strServer;
String strDomain = txtDomain.Text;
String strServerCom = "whois.networksolutions.com";
String strServerCN = "whois.cnnic.net.cn";
String strResponse;
string[] arrDomain = strDomain.Split('.');
if (arrDomain[1].ToUpper()=="CN")
{
       
        strServer=strServerCN;
}
else
{
        strServer=strServerCom;
}

bool blSuccess = IsWhosisSuccess(strDomain, strServer, out strResponse);
if (blSuccess)
{
lblResult.Text = strResponse;
}
else
{
lblResult.Text = "查找失败....";
}
}
bool IsWhosisSuccess(String strDomain, String strServer,
                   out String strResponse)
{
  strResponse = "none";
  bool blSuccess = false;
  TcpClient tcpc = new TcpClient();
  try
  {
    tcpc.Connect(strServer, 43);
  }
  catch(SocketException ex)
  {
    strResponse = "连接不到该 Whois server,请稍后再试。";
    return false;
  }

  strDomain += "\r\n";
  Byte[] arrDomain = Encoding.UTF8.GetBytes(strDomain.ToCharArray());
  try
  {
        Stream s = tcpc.GetStream();
        s.Write(arrDomain, 0, strDomain.Length);
       
        StreamReader sr = new StreamReader(tcpc.GetStream(), Encoding.UTF8);
        StringBuilder strBuilder = new StringBuilder();
        string strLine = null;

        while (null != (strLine = sr.ReadLine()))
        {
                strBuilder.Append(strLine+"<br>");
        }
        tcpc.Close();
               
        blSuccess = true;
        string my="Go to Huobazi's WebSite:<a href=\"http://www.github.com\" title=\".Net男孩社区\">www.github.Com</a><br>";
        strResponse = strBuilder.ToString()+my;  }
  catch(Exception e)
  {
        strResponse = e.ToString();
  }
  
  return blSuccess;
}
  </script>