.net杂记 ,调用EXE 获取路径 target VB DIV C# Split 页面之间传递值 更改session时间 EXCEL操作 控制电子邮件Mail input记录值的窍门

//获取路径中文件名

Path.GetFileName(pFullPath)

判断文件是否存在

File.Exists(路径文件)

控制fileUpload控件,不让更改里面的内容,只可以选择

contentEditable="false"

用来验证fileUpload控件中的上传的图片格式的javascript脚本

document.getElementById("FileUp").value!=""//判断控件里有没有字符

if(document.getElementById("FileUp").value!="")

{

var re=/\.(gif|jpg|bmp|png)$/i

if(re.test(document.getElementById("FileUp").value))

{

return true;

}

else

{

alert("本系统只支持gif|jpg|bmp|png类型的图片,谢谢!");

return false;

}

}

获取唯一ID

System.Guid.NewGuid()

//判断是否有指定的文件或文件夹 如果没有新建一个

if (!Directory.Exists(serverFile))

{

Directory.CreateDirectory(serverFile);

}

在html中调用javascript要在前面加上javascript例:<img id=’img2’ src=’…...’ onclick=”javascript:check()”/>

<a/>里target属性

_blank代表在新窗体打开页面

_self 在原窗体打开页面.

有三种局部变量

Const 常数局部变量,static静态局部变量,dim规则局部变量

Dim的应用(dim只在VB中可使用此变量)

<%

'声明变量

Dim I as Integer

' 设置字型的起始点数与截止点数

For I = 1 To 5

%>

<Font Size=<% =i %>>我的第一个ASP.NET程序<BR></Font>

<%

Next

Response.Write("编写第一个ASP.NET程序")

%>

C# 中字符串对应的方法:str.Split();

String str=”dingding”

String[] s=str.Split("n".ToCharArray());

现在:s[0]的值为:di

s[1]的值为:gdi

s[2]的值为:g

如果在字符串str中找不到n,只将整个字符串str返回。

TextBox获取焦点,失去焦点触发的事件

失去焦点:onblur

获得焦点:onfocus

但是它们只能执行javascript中的代码

如果想它们执行服务器端脚本,加一个隐藏的按钮。

<asp:TextBox runat="server" onblur="javascript:TxtOnBlur();"></asp:TextBox>

Function TxtOnBlur()

{

document.getElementById("btnOnBlur").click();

return ;

}

//把按钮放在一个隐藏层中

<div >

<input type="button" name="btnOnBlur" />

<div>

//服务器端

protected void btnOnBlur_ServerClick(object sender, EventArgs e)

{

Response.Write("dddddddd");

}

页面之间传递值

方式1:

在接收页 的html代码里加上一行: <%@ Reference Page = "WebForm1.aspx" %>

WebForm1 fp=(WebForm1)Context.Handler;

this.TextBox1.Text=fp.name; //name 是第一页的public变量

Context 提供对整个当前上下文(包括请求对象)的访问。您可以使用此类共享页之间的信息。

方式2:点击显示将aa.htm页面中的txtName值转给第一个页面bb.aspx的txtValue中

第一个页面:bb.aspx

function B()

{

var newWin=window.open("aa.htm");

}

<input />

<input type="button" />

页面aa.htm

function B()

{

//开启的窗体(bb.aspx)->开启的窗体文档->开启的窗体form->开启的窗体中的控件->控件中的值

window.opener.document.myForm.txtValue.value=document.form1.txtName.value;

}

<input type="text" />

<input type="button" />

方式3:打开一个对话窗。aa.htm当关闭aa.htm时页面中的txtName值转给第一个页面bb.aspx的txtValue中

第一个页面bb.aspx

function aa()

{

var newWin=window.showModalDialog("aa.htm");

var x=document.getElementById("txtValue");

// var x=document.all["txtValue"].value;

x.value=newWin;

}

<input />

<input value="打开对话框" type=button onclick="aa()"/>

第二个页面aa.htm

function A()

{

window.returnValue=document.form1.txtName.value;

window.close();

}

<input type="text" />

<input type="button" />

更改session时间

<sessionState mode="InProc" cookieless="false" regenerateExpiredSession timeout="200"/>

String.Format用法

String.Format

将指定的 String 中的每个格式项替换为相应对象的值的文本等效项。

例子:

int iVisit = 100;

string szName = "Jackfled";

Response.Write(String.Format("您的帐号是:{0} 。访问了 {1} 次.", szName, iVisit));

获取本地文件名:

string[] files = Directory.GetFiles("E:\\海南");//得到的信息为: E:\海南之旅1\DSCN3477.JPG

for (int i = 0; i < files.Length; i++)

{

string f ="";

f = files[i];

try

{

File.Move(f, "E:\\海南\\" + i.ToString() + ".JPG");

}

catch(Exception err)

{

Response.Write("<SCRIPT>alert('有错!');</SCRIPT>");

return;

}

}

。net 中调用一个可执行文件

System.Diagnostics.Process proc = new System.Diagnostics.Process();

proc.StartInfo.FileName = @"E:\123\123.bat";

proc.Start();

proc.WaitForExit();

EXCEL

Delphi 操作EXCEL —— EXCEL workbook.saveas 函数详解


本问所有资料来自于 Excel2003 VBA帮助文件,张荣整理,适用于DELPHI,VB的高级语言操作Excel用

ExcelApplication.WorkBook.SaveAs(filename,FileFormat, Password, WriteResPassword, ReadOnlyRecommended, CreateBackup, AccessMode, ConflictResolution, AddToMru, TextCodePage, TextVisualLayout ,Local)

1、Filename: Variant 类型,可选。该字符串表示要保存的文件名。可包含完整路径。如果不指定路径,Microsoft Excel 将文件保存到当前文件夹中。

2、FileFormat: Variant 类型,可选。保存文件时使用的文件格式。要得到有效选项的列表,请参阅 FileFormat 属性。对于已有文件,其默认格式是上次指定的文件格式;对于新文件,默认格式为当前使用的 Excel 版本格式 ,可选常数如下:

xlAddIn 18

xlCSV 6

xlCSVMac 22

xlCSVMSDOS 24

xlCSVWindows 23

xlCurrentPlatformText -4158

xlDBF2 7

xlDBF3 8

xlDBF4 11

xlDIF 9

xlExcel2 16

xlExcel2FarEast 27

xlExcel3 29

xlExcel4 33

xlExcel4Workbook 35

xlExcel5 39

xlExcel7 39

xlExcel9795 43

xlHtml 44

xlIntlAddIn 26

xlIntlMacro 25

xlSYLK 2

xlTemplate 17

xlTextMac 19

xlTextMSDOS 21

xlTextPrinter 36

xlTextWindows 20

xlUnicodeText 42

xlWebArchive 45

xlWJ2WD1 14

xlWJ3 40

xlWJ3FJ3 41

xlWK1 5

xlWK1ALL 31

xlWK1FMT 30

xlWK3 15

xlWK3FM3 32

xlWK4 38

xlWKS 4

xlWorkbookNormal -4143

xlWorks2FarEast 28

xlWQ1 34

xlXMLData 47

xlXMLSpreadsheet 46

3、Password :Variant 类型,可选。它是一个区分大小写的字符串(最长不超过 15 个字符),用于指定文件的保护密码。

4、WriteResPassword :Variant 类型,可选。 该字符串表示文件的写保护密码。如果文件保存时带有密码,但打开文件时不输入密码,则该文件以只读方式打开。

5、ReadOnlyRecommended : Variant 类型,可选。如果该值为 True,则在打开文件时显示一条信息,提示该文件以只读方式打开。

6、CreateBackup: Variant 类型,可选。如果该值为 True,则创建备份文件。

7、AccessMode :XlSaveAsAccessMode 类型,可选,如果省略此参数,则不会更改访问方式。如果在没有改变文件名的情况下保存共享列表,则可以忽略参数。若要改变访问方式,请使用 ExclusiveAccess 方法。 以下是可选常量:

xlExclusive 3 不含方式

xlNoChange 1 不更改访问方式,缺省值

xlShared 2 共享列表

8、ConflictResolution : XlSaveConflictResolution 类型,如果忽略本参数,则显示冲突处理对话框。可选常量如下:

xlLocalSessionChanges 2 自动接受本地用户的修改

xlOtherSessionChanges 3 接受除本地用户之外的其他用户的更改

xlUserResolution 1 显示冲突解决方案对话框

9、AddToMru :Variant 类型,可选。如果该值为 True ,则将该工作簿添加到最近使用的文件列表中。默认值为 False。

10、TextCodePage :Variant 类型,可选。不在美国英语版的 Microsoft Excel 中使用。

11、TextVisualLayout :Variant 类型,可选。不在美国英语版的 Microsoft Excel 中使用。

12、Local :Variant 类型,可选。如果该值为 True,则以 Microsoft Excel(包括控制面板设置)的语言保存文件。如果该值为 False(默认值),则以 Visual Basic for Applications (VBA) 的语言保存文件,其中 Visual Basic for Applications (VBA) 为典型安装的美国英语版本,除非 VBA 项目中的 Workbooks.Open 来自旧的国际化的 XL5/95 VBA 项目

__________________

.net 复制工作簿中的整个表

Excel.Application ThisApplication = new Excel.Application();

Excel.Workbook wb = ThisApplication.Workbooks.Open("e:\\test\\debug出口耗用明细表.xls", 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);

ThisApplication.Visible=false;

//获得第一个表 (这个例子是将自己工作簿中的表复制给自己,

//还可以改变get_Item(1)来指定另一个表)

Excel.Worksheet xlSheet = (Excel.Worksheet)wb.Worksheets.get_Item(1);

Excel.Range range = xlSheet.get_Range("A1", Type.Missing);

Excel.Worksheet sheet1 = (Excel.Worksheet)wb.Worksheets.get_Item(1);

Excel.Range range1 = sheet1.get_Range("B1", Type.Missing);

range.Copy(range1);

//这里可以用另存为

wb.Save();

关于。Net中解决长数值类型,会发生科学计算法或有e及0001的问题

//要导入using excel 和using System.Reflection;命名空间

第一步是:将长数值型的字符串前面加一个单引号,这是excel将数值转为字符的方法,不过显示的时候前面会有单引号

所以下面的这个方法是解决单引号问题

private void manageExcel()

{

Excel.ApplicationClass exc = new Excel.ApplicationClass();

if (exc == null)

{

Console.WriteLine("ERROR: EXCEL couldn't be started");

return;

}

object oMissing = Missing.Value;

exc.Visible=false;

Excel.Workbooks workbooks = exc.Workbooks;

string strOutFileName = PublicFun.m_strExportFilrPath + "\\" + UserInfo.UserID + "出口耗用明细表.xls";

Excel.Workbook wb = workbooks.Open(strOutFileName, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing , oMissing, oMissing, oMissing, oMissing, oMissing);

Excel._Worksheet ws= (Excel._Worksheet)wb.Sheets[1];

ws.Cells.NumberFormatLocal="@";

//number要改的列的所在的最后一行 注意i不可以为0

int number=dtOut.Rows.Count+3;

//有单引号的列要激活一下,单引号就会消失

for(int i=3;i<=number;i++)

{

ws.Cells[i,1]= ws.Cells[i,1];

}

wb.Save();

exc.Quit();

}

将现有的excel另存一份

Excel.ApplicationClass exc = new Excel.ApplicationClass();

if (exc == null)

{

Console.WriteLine("ERROR: EXCEL couldn't be started");

//return;

}

object oMissing = Missing.Value;

Excel.Workbooks workbooks = exc.Workbooks;

Excel.Workbook wb = workbooks.Open(strOutFileName, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing , oMissing, oMissing, oMissing, oMissing, oMissing);

wb.SaveAs(NewName,oMissing, oMissing, oMissing, oMissing, oMissing, Excel.XlSaveAsAccessMode.xlShared, oMissing , oMissing, oMissing, oMissing);

try

{

File.Delete(strOutFileName);

}

catch{}

linkExport.NavigateUrl = "..\\ExportFile\\" + strUserID + strFileName + strFileExp;

linkExport.Target = "_blank";

return strRtnValue;

//**********************************************************************************

///将dt以流方法写入,再保存后缀名为xls的文件,这样速度快,但是它并不是一个真正的excel文件

///其实它还是个txt文件(因为以流写入的都是txt文件);所为要把它以xml格式打开,再另存为一下,

///这样就可以把文件变成真正的excel文件(另存为是最主要的是第七个参数Excel.XlSaveAsAccessMode.xlShared)这种方法当数据大时会出问题,

注意:方法二:可以将数据以表格的形式输入。这样速度很快

// 四个参数分别为:strFileName文件名,excel的头,excel的结尾,一个超链接

public static string ExportData(string strFileName,string strHeadLine,string strLastLine,

System.Data.DataTable dtOut,System.Web.UI.WebControls.HyperLink linkExport)

{

string strOutFileName = ""; string strFileExp = ".xls";

string strLine = ""; string strRtnValue = "";

string [] arrTemp = strHeadLine.Split('~');

string NewName = m_strExportFilrPath + "\\" + strFileName + strFileExp;

FileStream objFileStream; StreamWriter objStreamWriter;

try

{

strOutFileName = m_strExportFilrPath + "\\ " + strFileName + strFileExp;

try

{

File.Delete(strOutFileName);

File.Delete(NewName);

}

catch{}

objFileStream = new FileStream(strOutFileName,FileMode.OpenOrCreate, FileAccess.Write);

objStreamWriter = new StreamWriter(objFileStream,System.Text.Encoding.Unicode);

try

{

// 输出表头信息 **********************************************************************************

try

{

if (arrTemp.Length>1) // 先输出文件信息

{

for (int intLoop=0; intLoop<arrTemp.Length-1; intLoop++) //输出表头信息

{

objStreamWriter.WriteLine(arrTemp[intLoop]);

}

objStreamWriter.WriteLine("");

objStreamWriter.WriteLine(arrTemp[arrTemp.Length-1]);

}

else

objStreamWriter.WriteLine(arrTemp[0]);

}

catch{}

// 输出表格内容 **********************************************************************************

try

{

for(int intLoop=0; intLoop<dtOut.Rows.Count; intLoop++)

{

for (int i = 0; i < dtOut.Columns.Count; i++)

{

strLine = strLine + dtOut.Rows[intLoop][i].ToString() + Convert.ToChar(9);

}

objStreamWriter.WriteLine(strLine);

strLine = "";

}

}

catch{}

// 输出表格尾信息 ********************************************************************************

try

{

strLine = "";

objStreamWriter.WriteLine(strLine);

objStreamWriter.WriteLine(strLine);

arrTemp = strLastLine.Split('~');

for (int intLoop=0; intLoop<arrTemp.Length; intLoop++)

{

if (arrTemp[intLoop]!="") objStreamWriter.WriteLine(arrTemp[intLoop]);

}

strLine = "制表人:" + Sicsoft.IETS.ThisApplication.CurrentUser.UserName;

objStreamWriter.WriteLine(strLine);

strLine = "制表日期:" + System.DateTime.Now.Year.ToString() + "-" + System.DateTime.Now.Month.ToString().PadLeft(2,'0') + "-" + System.DateTime.Now.Day.ToString().PadLeft(2,'0');

objStreamWriter.WriteLine(strLine);

}

catch{}

}

catch(Exception e)

{

strRtnValue = e.Message;

}

objStreamWriter.Close();

objFileStream.Close();

}

catch(Exception e)

{

strRtnValue = e.Message;

}

///xbding 增加 重新保存

Excel.ApplicationClass exc = new Excel.ApplicationClass();

if (exc == null)

{

Console.WriteLine("ERROR: EXCEL couldn't be started");

//return;

}

object oMissing = Missing.Value;

Excel.Workbooks workbooks = exc.Workbooks;

Excel.Workbook wb = workbooks.Open(strOutFileName, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing , oMissing, oMissing, oMissing, oMissing, oMissing);

wb.SaveAs(NewName,oMissing, oMissing, oMissing, oMissing, oMissing, Excel.XlSaveAsAccessMode.xlShared, oMissing , oMissing, oMissing, oMissing);

try

{

File.Delete(strOutFileName);

}

catch{}

linkExport.NavigateUrl = "..\\ExportFile\\" + strFileName + strFileExp;

linkExport.Target = "_blank";

return strRtnValue;

}

vs2002 ComboBox使用

comCoustoms.DataSource=dt;

comCoustoms.ValueMember="value";真正的值

comCoustoms.DisplayMember="Text";显示的值

comCoustoms.SelectedIndex=0;

访问安全问题

//如果发生安全问题财要在配制文件中加上在<configuration>之间加上 <system.web> <identity impersonate="true"/> </system.web>

控制电子邮件Mail

MailMessage mm = new MailMessage();

mm.From = "qisheng@dcjet.com.cn";

mm.To = "xbding@dcjet.com.cn";

mm.Subject = "Microsoft Office Outlook 测试消息";

mm.Body = "我要日你";

SmtpMail.SmtpServer = "dcjet.com.cn";

SmtpMail.Send(mm);

Response.Write("<script>alert('发送成功!')</script>");

控制电子邮件Mail 2

MailMessage mm = new MailMessage();

mm.From = "ding@dcjet.com.cn";

mm.To = "xbding@dcjet.com.cn";

mm.Subject = "Microsoft Office Outlook 测试消息";

mm.Body = "我你";

SmtpMail.SmtpServer = "dcjet.com.cn";

SmtpMail.Send(mm);

Response.Write("<script>alert('发送成功!')</script>");

ASP小巧门

在写页面的时候,<input >里不用id,只用name。这样页面文本框输入值提交过后会被记录下来。

下次再输入第一个字符相同时就会把以前输入首字符相同的数值自动以下拉列表形式显示出来。

将日期转变指定格式:

String(“yyMMdd”);