.Net知识积累
一.Url编码
HttpUtility.UrlEncode(text); //utf-8 编码
HttpUtility.UrlDecode(text); //utf-8 解码
HttpUtility.UrlEncode(text, System.Text.Encoding.GetEncoding(936)); //gb2312编码
HttpUtility.UrlDecode(text, System.Text.Encoding.GetEncoding(936)); //gb2312解码
二.对象转json
Hashtable hash1 = new Hashtable();
hash1["createdate"] = dt.Rows[i]["CREATEDATE"].ToString();
JavaScriptSerializer ser = new JavaScriptSerializer();
ansJsonStr = ser.Serialize(hash1);
三.dataSet和DataTable 关系
dateSet–属于数据库返回多表(多个游标返回)或者单表(单个游标)暂时存放在缓存中
DataTable–是dataSet的子类
DataSet ds=new DateSet()
DataTable dt2=new DataTable();
dt2.TableName="dt2n";
ds.Tables.Add(dt2);
DataSet ds = new DataSet();//创建数据集
DataTable dt1=new DataTable(); //表1
DataTable dt2 = new DataTable(); //表2
给table 取名再插 于是代码便成这样
dt1.TableName="dt1n";
ds.Tables.Add(dt1);
dt2.TableName="dt2n";
ds.Tables.Add(dt2);
dt.Columns("ColumnName").SetOrdinal(4) //将栏位顺序调到第4列
1.获取表
DataTable dt=ds.Tables[0];0表示(多个表取第一个)
2.修改DataTable表名
Ds.Tables[0].TableName = "隐患";
3.获取DataTable中所有字段的数量
int i=dt.Columns.Count
4.获取表中总行数
int aa=dt.Rows.Count;
5.获取DataTable表中第三个字段的名称(列明)
string a=dt.Columns[2].ColumnName;
6.获取DataTable表第0行第1个字段的值
string b=dt.Rows[0][0].ToString();
7.获取DataTable表第0行第10个字段的值
string b=dt.Row[0][9].Tostring();
四.web请求处理
byte[] data="";
string posturl= "";
string PostData = PostData.Replace("'", "\"").Replace(" ", "").Replace("\n", "");
data = System.Text.UTF8Encoding.UTF8.GetBytes(PostData);
web.UploadData(posturl,data);//post 请求
web.DownloadData( posturl);//get 请求
web.DownloadFile(posturl,file_name); //获取流文件
五.对文件信息进行处理
using System.IO; //引入命名空间
string path = "text.aspx";
string pathName = Path.GetFileName(path); //获取文件全名 text.aspx
string pathName1 = Paht.GetFileNameWithoutExtension(path); //获取文件名 text
string pathName2 = Path.GetExtension(path); //获取文件后缀名 aspx
string pathName3 =Path.GetFileNameWithoutExtension(filename);//获取无后缀的文件名
DirectoryInfo theFolder = new DirectoryInfo(@whzfilename);
FileSystemInfo[] files = theFolder.GetFileSystemInfos();//查询当前目录下的所有文件及文件夹
files =theFolder.GetDirectories("*新建*");//查询当前目录下的文件名叫查勘表的文件
theFolder.GetFiles("*查勘表*");//这个是查子文件中名字为查勘表的文件
string[] folders = Directory.GetDirectories(@whzfilename);//这个是查子文件夹,
string[] files1 = Directory.GetFiles(whzfilename, "*查勘表*");//这个是查子文件中名字为查勘表的文件
六.前后台数据交互
//1)返回json对象
json.addItem(DataSet);
return json.readData();
//2)返回HTML
StringBuffder sb;
sp.Append("tr")
return sb.ToString();
//3)返回json表格
js.AddItem(DataSet);
return js.ToString();
七.时间处理
//DateTime 数字型
System.DateTime currentTime=new System.DateTime();
取当前年月日时分秒 currentTime=System.DateTime.Now;
取当前年 int 年=currentTime.Year;
取当前月 int 月=currentTime.Month;
取当前日 int 日=currentTime.Day;
取当前时 int 时=currentTime.Hour;
取当前分 int 分=currentTime.Minute;
取当前秒 int 秒=currentTime.Second;
八.Execl 处理
using Aspose.Cells;
Workbook wb = new Workbook();//创建工作簿
Worksheet sheet;
sheet = wb.Worksheets[0];//创建sheet
sheet.Name = sheetName;//修改该sheet的名字
Cells cells = sheet.Cells;//声明单元格
cells.Merge(oldCount, 12, y , x);
//其中oldCount为合并开始列,如我需要将2M-8M进行合并那么oldCount应为2M所在的行数既为:2
//12表示合并开始行,如我需要将2M-8M进行合并那么12就为2M所在的列数即为:12
//y表示合并多少列,如2M-8M共7列
//x表示合并多少行,如2M-8M共1行
cells.HideColumn(5);//隐藏第五列
cells.HideColumns(7, 2);//隐藏多行 隐藏第八个,向右两个
cells.Merge(0, 0, 1, 26);//合并单元格
cells[0, 0].PutValue("工资发放表");//A1单元格内容为工资发放表
cells[0, 0].Value = "单位主管";//A1单元格内容为单位主管
cells[0, 0].SetStyle(styleTitle);//设置样式为styleTitle
cells.SetRowHeight(1, 20);//设置行第二行高度为20 (从0开始)
cells[4 + l, k].Formula=(=COUNTA(A1:A2));//设置公式 计数A1:A2