Q:如何将WEB页面中的数据保存为一个word文件
A:直接用word打开,然后存成.doc文件就可以了。
protected void ConvertToHtml(string SrcFilePath,string TargetFilePath)
{
Word.Application app=new Word.Application();
app.Visible=false;
Object o=Missing.Value;
object docFile=SrcFilePath;
_Document doc=app.Documents.Open(ref docFile,ref o,ref o,ref o,ref o,ref o,ref o,ref o,ref o,ref o,ref o,ref o,ref o,ref o,ref o);
object fileName=TargetFilePath;
object format=Word.WdSaveFormat.wdFormatDocument; //word format
doc.SaveAs(ref fileName,ref format,ref o,ref o,ref o,ref o,ref o,ref o,ref o,ref o,ref o,ref o,ref o,ref o,ref o,ref o);
object t=true;
app.Quit(ref t,ref o,ref o);
}
Q:在Web上如何实现水晶报表的打印!!如何实现将导出文件保存到本地
A:crReportDocument.SetDataSource(Ds);
ExportOptions crExportOptions; //导出选项
DiskFileDestinationOptions crDiskFileDestinationOptions;
String Fname; //导出完整名称
//String FileName = "";
String FileName = "my.pdf";
String _SystemPath =Request.PhysicalApplicationPath;
if(!System.IO.Directory.Exists(_SystemPath))
System.IO.Directory.CreateDirectory(_SystemPath);
Fname = _SystemPath + FileName; //导出完整名称
crDiskFileDestinationOptions = new DiskFileDestinationOptions();
crDiskFileDestinationOptions.DiskFileName = Fname;
crExportOptions = crReportDocument.ExportOptions;
crExportOptions.DestinationOptions = crDiskFileDestinationOptions;
crExportOptions.ExportDestinationType = ExportDestinationType.DiskFile;
crExportOptions.ExportFormatType = ExportFormatType.PortableDocFormat;
crReportDocument.Export();
Q:如何读取JPG或GIF图片二进制格式?
A:Byte[] Data;
_fileStream = new FileStream(fullPath,System.IO.FileMode.Open);
//再将该文件流转换为Byte类型
Data=new Byte[_fileStream.Length];
_fileStream.Read(Data,0,(int)_fileStream.Length);
在转为binary[]
Q:C#中调用WIN32 API实现对光驱的操作
A:
using System;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.Runtime.InteropServices;
class CDRomControler:Form
{
[DllImport( "winmm.dll", EntryPoint="mciSendStringA", CharSet=CharSet.Ansi )]
public static extern int mciSendString( string lpstrCommand, StringBuilder lpstrReturnString, int uReturnLength, IntPtr hwndCallback );
private Button openButton;
private Button closeButton;
public CDRomControler()
{
this.AutoScaleBaseSize=new Size(5,13);
this.ClientSize=new Size(400,200);
this.Text="C#操作光驱";
this.openButton=new Button();
openButton.Location=new Point(50,100);
openButton.Size=new Size(100,20);
openButton.TabIndex=1;
openButton.Anchor=AnchorStyles.Bottom|AnchorStyles.Left;
openButton.Text="打开光驱";
openButton.Click+=new EventHandler(this.openButton_Click);
this.Controls.Add(this.openButton);
this.closeButton=new Button();
closeButton.Location=new Point(250,100);
closeButton.Size=new Size(100,20);
closeButton.TabIndex=2;
closeButton.Anchor=AnchorStyles.Bottom|AnchorStyles.Right;
closeButton.Text="关闭光驱";
closeButton.Click+=new EventHandler(this.closeButton_Click);
this.Controls.Add(this.closeButton);
}
private void openButton_Click(object sender, System.EventArgs e)
{
int ret = mciSendString( "set cdaudio door open", null, 0, IntPtr.Zero );
}
private void closeButton_Click(object sender, System.EventArgs e)
{
int ret = mciSendString( "set cdaudio door closed", null, 0, IntPtr.Zero );
}
public static void Main()
{
Application.Run(new CDRomControler()); ;
}
}
Q:C#捕捉视频/
A:
using System;
using System.Runtime.InteropServices;
namespace Wuyin.ShoesManager
{
/// <summary>
/// VedioCapture 的摘要说明。
/// </summary>
public class VedioCapture
{
private int hCaptureM;
private bool isUnLoad = false;
public VedioCapture()
{
}
[DllImport("avicap32.dll")]
private static extern int capCreateCaptureWindow( string strWindowName, int dwStyle, int x, int y ,int width, int height , int hwdParent, int nID );
[DllImport("user32.dll")]
private static extern int SendMessage( int hwnd , int wMsg, int wParam , int lParam );
[DllImport("user32.dll")]
private static extern int SendMessage( int hwnd , int wMsg, int wParam , string lParam );
[DllImport("Kernel32.dll")]
private static extern bool CloseHandle( int hObject );
public bool Initialize( System.Windows.Forms.Control aContainer , int intWidth, int intHeight )
{
hCaptureM = capCreateCaptureWindow( "", 0x40000000 | 0x10000000, 0,0,intWidth,intHeight,aContainer.Handle.ToInt32() ,1 );
if( hCaptureM == 0 ) return false;
int ret = SendMessage( hCaptureM , 1034, 0,0 );
if( ret == 0 )
{
CloseHandle(hCaptureM);
return false;
}
//WM_CAP_SET_PREVIEW
ret = SendMessage( hCaptureM, 1074, 1, 0 );
if( ret == 0 )
{
this.UnLoad();
return false;
}
//WM_CAP_SET_SCALE
ret = SendMessage( hCaptureM, 1077, 1, 0 );
if( ret == 0 )
{
this.UnLoad();
return false;
}
//WM_CAP_SET_PREVIEWRATE
ret = SendMessage( hCaptureM, 1076, 66, 0 );
if( ret == 0 )
{
this.UnLoad();
return false;
}
return true;
}
public void SingleFrameBegin()
{
//
int ret = SendMessage( hCaptureM, 1094 , 0, 0 );
}
public void SingleFrameEnd()
{
//
int ret = SendMessage( hCaptureM, 1095 , 0, 0 );
}
public void SingleFrameMode()
{
//WM_CAP_GRAB_FRAME
int ret = SendMessage( hCaptureM, 1084 , 0, 0 );
//WM_CAP_SET_PREVIEW
//int ret = SendMessage( hCaptureM, 1074 , 0, 0 );
//WM_CAP_SINGLE_FRAME
//ret = SendMessage( hCaptureM, 1096 , 0, 0 );
}
public void PreviewMode()
{
int ret = SendMessage( hCaptureM, 1074 , 1, 0 );
}
public void UnLoad()
{
int ret = SendMessage( hCaptureM, 1035, 0, 0 );
CloseHandle( this.hCaptureM );
isUnLoad = true;
}
public void CopyToClipBorad()
{
int ret = SendMessage( hCaptureM, 1054, 0, 0 );
}
public void ShowFormatDialog()
{
int ret = SendMessage( hCaptureM, 1065, 0, 0 );
}
public void SaveToDIB( string fileName )
{
int ret = SendMessage( hCaptureM, 1049, 0, fileName );
}
public void ShowDisplayDialog()
{
int ret = SendMessage( hCaptureM, 1067, 0, 0 );
}
public System.Drawing.Image getCaptureImage()
{
System.Windows.Forms.IDataObject iData = System.Windows.Forms.Clipboard.GetDataObject();
System.Drawing.Image retImage = null;
if( iData != null )
{
if( iData.GetDataPresent( System.Windows.Forms.DataFormats.Bitmap ) )
{
retImage = (System.Drawing.Image)iData.GetData( System.Windows.Forms.DataFormats.Bitmap );
}
else if( iData.GetDataPresent( System.Windows.Forms.DataFormats.Dib ) )
{
retImage = (System.Drawing.Image)iData.GetData( System.Windows.Forms.DataFormats.Dib );
}
}
return retImage;
}
~VedioCapture()
{
if( !isUnLoad )
{
this.UnLoad();
}
}
}
}
Q:存图片到数据库
A:using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;
using System.IO;
namespace CExample
{
public class ImageDB : System.Windows.Forms.Form
{
private System.Windows.Forms.Button BtnAdd;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.TextBox EdtID;
private System.Windows.Forms.TextBox EdtImage;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Button BtnShow;
private System.Windows.Forms.PictureBox StudentPhoto;
...
private void BtnAdd_Click(object sender, System.EventArgs e)
{
AddStudentPhoto(102, @"C:/Temp/1.jpg");
}
private void AddStudentPhoto(int Student_ID, string ImageFile)
{
FileStream myFile = new FileStream(ImageFile, FileMode.Open, FileAccess.Read);
byte[] Photo = new byte[myFile.Length];
myFile.Read(Photo, 0, (int)myFile.Length);
myFile.Close();
SavePhotoToDB(Student_ID, Photo);
}
private void SavePhotoToDB(int Student_ID, byte[] Photo)
{
string ConnectString = "data source=Hellcat;initial catalog=SMS;" +
"User ID=***;password=***";
using (SqlConnection myCon = new SqlConnection(ConnectString) )
{
if (myCon.State != ConnectionState.Open) //if connection is not open, open it
myCon.Open();
SqlCommand myCmd = new SqlCommand("AddStudentPhoto", myCon);
myCmd.CommandType = CommandType.StoredProcedure;
myCmd.Parameters.Add(new SqlParameter("@Student_ID", SqlDbType.Int, 0));
myCmd.Parameters.Add(new SqlParameter("@Photo", SqlDbType.Image));
myCmd.Parameters["@Student_ID"].Value = Student_ID;
myCmd.Parameters["@Photo"].Value = Photo;
myCmd.ExecuteNonQuery();
myCon.Close();
MessageBox.Show("Add student photo successfully!");
}
}
}
}
Q:序列化对象
A:可以先建一个ArrayList,然后将需要序列化的对象加入到ArrayList,再序列化ArrayList就可以了。
恢复对象时,只要通过ArrayList索引就可以还原对象!
(具体实现大家体会,这个例子是我在CSDN上看到的,比较深刻)
namespace abc
{
[Serializable]class employee:ISerializable
{
private string name;
public string Name
{
set {name=value;}
get {return name;}
}
public override string ToString()
{
return name;
}
public void GetObjectData(SerializationInfo info, StreamingContext context)
{
info.AddValue("Name",name);
}
public employee(SerializationInfo info,StreamingContext context)
{
name=(string)info.GetValue("Name",typeof(string));
}
public employee(string name)
{
this.name=name;
}
}
class Class1
{
[STAThread]
static void Main(string[] args)
{
Stream w=File.Open("e://abc.dat",FileMode.OpenOrCreate,FileAccess.ReadWrite);
ArrayList Objects=new ArrayList();
Console.WriteLine("键入#,按回车退出输入");
while (true)
{
Console.WriteLine("请输入姓名:");
string name=Console.ReadLine();
if (name=="#")
break;
employee empl=new employee(name);
Objects.Add(name);
}
BinaryFormatter a=new BinaryFormatter();
a.Serialize(w,Objects);
w.Close();
Console.WriteLine("读取...");
Stream w1=File.Open("e://abc.dat",FileMode.Open,FileAccess.Read);
BinaryFormatter r=new BinaryFormatter();
object o=r.Deserialize(w1);
Console.WriteLine("键入#,按回车退出读取");
while (true)
{
Console.WriteLine("请选择要显示的姓名顺序号(键入顺序):");
string Index=Console.ReadLine();
if (Index=="#")
break;
int index=int.Parse(Index);
Console.WriteLine(((ArrayList)o)[index].ToString());
w1.Close();
}
}
}
}
Q:远程重启计算机
A;ConnectionOptions options = new ConnectionOptions ( ) ;
options.Username ="administrator";
options.Password = "" ;
ManagementScope scope = new ManagementScope( "" + "10.5.0.5" + "//root//cimv2", options ) ;
try
{
scope.Connect ( ) ;
System.Management.ObjectQuery oq = new System.Management.ObjectQuery ( "SELECT * FROM Win32_OperatingSystem" ) ;
ManagementObjectSearcher query1 = new ManagementObjectSearcher ( scope , oq ) ;
//得到WMI控制
ManagementObjectCollection queryCollection1 = query1.Get ( ) ;
foreach ( ManagementObject mo in queryCollection1 )
{
string [ ] ss= { "" } ;
mo.InvokeMethod ( "Reboot" , ss ) ;
}
}
catch
{
}