c#.net常用函数和方法集

、DateTime 数字型
System.DateTime currentTime=new System.DateTime();
1.1 取当前年月日时分秒
currentTime=System.DateTime.Now;
1.2 取当前年
int 年=currentTime.Year;
1.3 取当前月
int 月=currentTime.Month;
1.4 取当前日
int 日=currentTime.Day;
1.5 取当前时
int 时=currentTime.Hour;
1.6 取当前分
int 分=currentTime.Minute;
1.7 取当前秒
int 秒=currentTime.Second;
1.8 取当前毫秒
int 毫秒=currentTime.Millisecond;
(变量可用中文)
2、Int32.Parse(变量) Int32.Parse(”常量”)
字符型转换 转为32位数字型
3、 变量.ToString()
字符型转换 转为字符串
12345.ToString(”n”); //生成 12,345.00
12345.ToString(”C”); //生成 ¥12,345.00
12345.ToString(”e”); //生成 1.234500e+004
12345.ToString(”f4″); //生成 12345.0000
12345.ToString(”x”); //生成 3039 (16进制)
12345.ToString(”p”); //生成 1,234,500.00%
4、变量.Length 数字型
取字串长度:
如: string str=”中国”;
int Len = str.Length ; //Len是自定义变量, str是求测的字串的变量名
5、System.Text.Encoding.Default.GetBytes(变量)
字码转换 转为比***
如:byte[] bytStr = System.Text.Encoding.Default.GetBytes(str);
然后可得到比特长度:
len = bytStr.Length;
6、System.Text.StringBuilder(”")
字符串相加,(+号是不是也一样?)
如:System.Text.StringBuilder sb = new System.Text.StringBuilder(”");
sb.Append(”中华”);
sb.Append(”人民”);
sb.Append(”共和国”);
7、变量.Substring(参数1,参数2);
截取字串的一部分,参数1为左起始位数,参数2为截取几位。
如:string s1 = str.Substring(0,2);
8、String user_IP=Request.ServerVariables[”REMOTE_ADDR”].ToString();
取远程用户IP地址
9、穿过代理服务器取远程用户真实IP地址:
if(Request.ServerVariables[”HTTP_VIA”]!=null){
string user_IP=Request.ServerVariables[”HTTP_X_FORWARDED_FOR”].ToString();
}else{
string user_IP=Request.ServerVariables[”REMOTE_ADDR”].ToString();
}

10、 Session[”变量”];
存取Session值;
如,赋值: Session[”username”]=”小布什”;
取值: Object objName=Session[”username”];
String strName=objName.ToString();
清空: Session.RemoveAll();
11、String str=Request.QueryString[”变量”];
用超链接传送变量。
如在任一页中建超链接:“;点击
在Edit.aspx页中取值:String str=Request.QueryString[”fdid”];
12、DOC对象.CreateElement(”新建节点名”);
创建XML文档新节点
13、父节点.AppendChild(子节点);
将新建的子节点加到XML文档父节点下
14、 父节点.RemoveChild(节点);
删除节点
15、Response
Response.Write(”字串”);
Response.Write(变量);
向页面输出。
Response.Redirect(”URL地址”);
跳转到URL指定的页面
16、char.IsWhiteSpce(字串变量,位数)——逻辑型
查指定位置是否空字符;
如:
string str=”中国 人民”;
Response.Write(char.IsWhiteSpace(str,2)); //结果为:True, 第一个字符是0位,2是第三个字符。
17、char.IsPunctuation(’字符’) –逻辑型
查字符是否是标点符号
如:Response.Write(char.IsPunctuation(’A')); //返回:False
18、(int)’字符’
把字符转为数字,查代码点,注意是单引号。
如:
Response.Write((int)’中’); //结果为中字的代码:20013
19、(char)代码
把数字转为字符,查代码代表的字符。
如:
Response.Write((char)22269); //返回“国”字。

20、 Trim()
清除字串前后空格
21 、字串变量.Replace(”子字串”,”替换为”)
字串替换
如:
string str=”中国”;
str=str.Replace(”国”,”央”); //将国字换为央字
Response.Write(str); //输出结果为“中央”
再如:(这个非常实用)
string str=”这是脚本”;
str=str.Replace(”"); //将左尖括号替换为 与 (或换为&lt,但估计经XML存诸后,再提出仍会还原)
Response.Write(str); //显示为:“这是脚本”
如果不替换,将不显示,如果是一段脚本,将运行;而替换后,脚本将不运行。
这段代码的价值在于:你可以让一个文本中的所有HTML标签失效,全部显示出来,保护你的具有交互性的站点。
具体实现:将你的表单提交按钮脚本加上下面代码:
string strSubmit=label1.Text; //label1是你让用户提交数据的控件ID。
strSubmit=strSubmit.Replace(”");
然后保存或输出strSubmit。
用此方法还可以简单实现UBB代码。
22、Math.Max(i,j)
取i与j中的最大值
如 int x=Math.Max(5,10); // x将取值 10


C#也能动态生成Word文档并填充数据
Category : DotNet技术 | Post on 2007/09/15 19:34 by gdgzboy | Comments:0
[此文来源于互联网,牛C网只负责收集整理]

  要使用C#操作word,首先要添加引用:
1、添加引用->COM->Microsoft Word 11.0 Object Library

2、在.cs文件中添加

using Word;
下面的例子中包括C#对Word文档的创建、插入表格、设置样式等操作:

(例子中代码有些涉及数据信息部分被省略,重要是介绍一些C#操作word文档的方法)

public string CreateWordFile(string CheckedInfo)
...{
string message = "";
try
...{
Object Nothing = System.Reflection.Missing.Value;
Directory.CreateDirectory("C:/CNSI"); //创建文件所在目录
string name = "CNSI_" DateTime.Now.ToShortString() ".doc";
object filename = "C://CNSI//" name; //文件保存路径
//创建Word文档
Word.Application WordApp = new Word.ApplicationClass();
Word.Document WordDoc = WordApp.Documents.Add(ref Nothing, ref Nothing, ref Nothing, ref Nothing);

//添加页眉
WordApp.ActiveWindow.View.Type = WdViewType.wdOutlineView;
WordApp.ActiveWindow.View.SeekView = WdSeekView.wdSeekPrimaryHeader;
WordApp.ActiveWindow.ActivePane.Selection.InsertAfter("[页眉内容]");
WordApp.Selection.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphRight;//设置右对齐
WordApp.ActiveWindow.View.SeekView = WdSeekView.wdSeekMainDocument;//跳出页眉设置

WordApp.Selection.ParagraphFormat.LineSpacing = 15f;//设置文档的行间距

//移动焦点并换行
object count = 14;
object WdLine = Word.WdUnits.wdLine;//换一行;
WordApp.Selection.MoveDown(ref WdLine, ref count, ref Nothing);//移动焦点
WordApp.Selection.TypeParagraph();//插入段落

//文档中创建表格
Word.Table newTable = WordDoc.Tables.Add(WordApp.Selection.Range, 12, 3, ref Nothing, ref Nothing);
//设置表格样式
newTable.Borders.OutsideLineStyle = Word.WdLineStyle.wdLineStyleThickThinLargeGap;
newTable.Borders.InsideLineStyle = Word.WdLineStyle.wdLineStyleSingle;
newTable.Columns[1].Width = 100f;
newTable.Columns[2].Width = 220f;
newTable.Columns[3].Width = 105f;

//填充表格内容
newTable.Cell(1, 1).Range.Text = "产品详细信息表";
newTable.Cell(1, 1).Range.Bold = 2;//设置单元格中字体为粗体
//合并单元格
newTable.Cell(1, 1).Merge(newTable.Cell(1, 3));
WordApp.Selection.Cells.VerticalAlignment = Word.WdCellVerticalAlignment.wdCellAlignVerticalCenter;//垂直居中
WordApp.Selection.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter;//水平居中

//填充表格内容
newTable.Cell(2, 1).Range.Text = "产品基本信息";
newTable.Cell(2, 1).Range.Font.Color = Word.WdColor.wdColorDarkBlue;//设置单元格内字体颜色
//合并单元格
newTable.Cell(2, 1).Merge(newTable.Cell(2, 3));
WordApp.Selection.Cells.VerticalAlignment = Word.WdCellVerticalAlignment.wdCellAlignVerticalCenter;

//填充表格内容
newTable.Cell(3, 1).Range.Text = "品牌名称:";
newTable.Cell(3, 2).Range.Text = BrandName;
//纵向合并单元格
newTable.Cell(3, 3).Select();//选中一行
object moveUnit = Word.WdUnits.wdLine;
object moveCount = 5;
object moveExtend = Word.WdMovementType.wdExtend;
WordApp.Selection.MoveDown(ref moveUnit, ref moveCount, ref moveExtend);
WordApp.Selection.Cells.Merge();
//插入图片
string FileName = Picture;//图片所在路径
object LinkToFile = false;
object SaveWithDocument = true;
object Anchor = WordDoc.Application.Selection.Range;
WordDoc.Application.ActiveDocument.InlineShapes.AddPicture(FileName, ref LinkToFile, ref SaveWithDocument, ref Anchor);
WordDoc.Application.ActiveDocument.InlineShapes[1].Width = 100f;//图片宽度
WordDoc.Application.ActiveDocument.InlineShapes[1].Height = 100f;//图片高度
//将图片设置为四周环绕型
Word.Shape s = WordDoc.Application.ActiveDocument.InlineShapes[1].ConvertToShape();
s.WrapFormat.Type = Word.WdWrapType.wdWrapSquare;

newTable.Cell(12, 1).Range.Text = "产品特殊属性";
newTable.Cell(12, 1).Merge(newTable.Cell(12, 3));
//在表格中增加行
WordDoc.Content.Tables[1].Rows.Add(ref Nothing);

WordDoc.Paragraphs.Last.Range.Text = "文档创建时间:" DateTime.Now.ToString();//“落款”
WordDoc.Paragraphs.Last.Alignment = Word.WdParagraphAlignment.wdAlignParagraphRight;

//文件保存
WordDoc.SaveAs(ref filename, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing);
WordDoc.Close(ref Nothing, ref Nothing, ref Nothing);
WordApp.Quit(ref Nothing, ref Nothing, ref Nothing);
message=name "文档生成成功,以保存到C:CNSI下";
}
catch
...{
message = "文件导出异常!";
}
return message;
}

posted on 2008-05-27 11:43  hcmfys_lover  阅读(362)  评论(1编辑  收藏  举报