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

   2023-02-09 学习力0
核心提示://获取路径中文件名Path.GetFileName(pFullPath)判断文件是否存在File.Exists(路径文件)控制fileUpload控件,不让更改里面的内容,只可以选择contentEditable="false"用来验证fileUpload控件中的上传的图片格式的javascript脚本document.getElementById("FileUp

//获取路径中文件名

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 ID="MyTextBox" runat="server" onblur="javascript:TxtOnBlur();"></asp:TextBox>

Function TxtOnBlur()

{

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

return ;

}

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

<div style="display:none">

<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" regenerateExpiredSessionId="true" 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”);

 
反对 0举报 0 评论 0
 

免责声明:本文仅代表作者个人观点,与乐学笔记(本网)无关。其原创性以及文中陈述文字和内容未经本站证实,对本文以及其中全部或者部分内容、文字的真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
    本网站有部分内容均转载自其它媒体,转载目的在于传递更多信息,并不代表本网赞同其观点和对其真实性负责,若因作品内容、知识产权、版权和其他问题,请及时提供相关证明等材料并与我们留言联系,本网站将在规定时间内给予删除等相关处理.

  • Windows API Reference for C#, VB.NET
    不错的.net 下用API的参考站点地址在:http://www.webtropy.com/articles/Win32-API-DllImport-art9.asp 下面摘抄分类,便于大家直接就拿来用: File, Memory, Process, Threading, Time, Console, and Comm control(kernel32.dll) _hread_hwrite_lclose_lcr
    03-16
  • 一个基于API的VB.net串口通讯类 vbnet串口通信
    VB.net的串口通讯支持总是让人觉得有所不足,在使用VB6的MsComm32.ocx时,很多人都会在VB.net的开发中觉得很困扰。    这里讲述的VB.net串口通讯类使用native代码,并且它是通API调用实现的,你会发现VB.net的串口通讯就是这么简单。    在说明如何使
    02-12
  • [VB][ASP.NET]FileUpload控件「批次上传 / 多档
    FileUpload控件「批次上传 / 多档案同时上传」的范例 (VB语法) http://www.dotblogs.com.tw/mis2000lab/archive/2008/05/14/3986.aspx    FileUpload控件真的简单好用,不使用它来作批次上传,却要改用别的方法,实在不聪明。要用就一次用到底,公开File
    02-10
  • 第八章 VB中ActiveX控件的使用
    轉自:http://wwww.hyit.edu.cn/edu/vb/study/index.htm第八章          VB中ActiveX控件的使用8.1  概述     这里的ActiveX控件是指VB标准工具箱里没有的控件,用时需从“工程”菜单里选择“部件…”(或右键单击工具箱,从快捷菜单中选择“部
    02-10
  • 第二章 VB的界面设计
    轉自:http://wwww.hyit.edu.cn/edu/vb/study/index.htm第二章         VB的界面设计2.1  VB用户界面设计基础1. 概述   界面的设计有两步:先绘制控件,然后确定控件属性。   绘制控件:在工具箱里单击想画的控件,在窗体里按下鼠标并拖曳,然后
    02-10
  • C#/VB.NET 获取Excel中图片所在的行、列坐标位置
    C#/VB.NET 获取Excel中图片所在的行、列坐标位
    本文以C#和vb.net代码示例展示如何来获取Excel工作表中图片的坐标位置。这里的坐标位置是指图片左上角顶点所在的单元格行和列位置,横坐标即顶点所在的第几列、纵坐标即顶点所在的第几行。下面是获取图片位置的详细方法及步骤。【程序环境】按照如下方法来引
    02-09
  • VB操作XML
    VB操作XML
    XSL(可扩展样式表语言)是对CSS的一种扩展,功能比CSS强大得多。XML链接是在HTML链接的功能之上加以扩展,可以支持更为复杂的链接,通过XML链接,不仅可以在XML文件之间建立链接,还可以建立其他类型数据之间的链接,其规范分为三个部分:XLink语言,XPointe
    02-09
  • VB6多线程,关键段操作 vb6.0 多线程
    Option Explicit Declare Function GetLastError Lib "kernel32" () As Long 'Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long 'Declare Sub ExitThread Lib "kernel32" (Optional ByVal dwExitCode
    02-09
  • VB.NET调用IE,并且等待
                Dim p As New Process            '获得URL            aURL = GetURL()            '获得IE路径            p.StartInfo.FileName = System.Environment.GetFolderPath( _ 
    02-09
  • vb的VSFlexGrid控件 vb msflexgrid
    多行选中VSFlexGrid的SelectionMode = flexSelectionListBox,现在可以配合Ctrl进行多行选择循环取值用vsflexgrid.SelectedRows 可以得到你选择的行的总数量然后用循环可以得到具体的行中具体列的内容Dim Temp  As StringDim i As IntegerFor i =
    02-09
点击排行