ADO Excel Templates
private static void Excute ()
{
while (true)
{
String TemplatePath = System.Environment.CurrentDirectory + "\\template.xls";
String NewPath = System.Environment.CurrentDirectory + "\\temp.xls";
if (! File.exists (TemplatePath))
return;
if (! File.exists (NewPath))
file.copy (TemplatePath, newpath,true);
FileInfo FileInfo = New FileInfo (NewPath);
String s = FileInfo.Attributes.ToString ();
if ( FileInfo.Attributes.ToString (). ToLower (). IndexOf ("readonly")! =-1)
{
fileinfo.attributes = Fileattributes.normal;
}
String connstr = " provider=microsoft.jet.oledb.4.0; "+
"Extended properties=\" Excel 8.0; Hdr=yes;imex=2\ ";" +
//Specify Extended Properties for Microsoft Excel 8.0 (97) 9.0 (2000) 10.0 (2002), and the first row is returned as data and is read as text & nbsp;
"Data source=" + NewPath;
OleDbConnection conn = new OleDbConnection (CONNSTR);
OleDbCommand cmd = new OleDbCommand ("", conn);
Try
{
Conn. Open ();
String sql = "INSERT INTO [sheet2$] (name, gender, contact phone) VALUES (41,51,61)";
Cmd.commandtext = SQL;
Cmd. ExecuteNonQuery ();
Conn. Close ();
FileStream reader = File.openread (NewPath);
int length = (int) reader. Length;
byte[] buffer = new Byte[length];
int bytetoread;
while (length > 0)
//{
The client is still open.
Bytetoread = reader. Read (buffer, 0, length);
Length-= Bytetoread;
When the client disconnects
length =-1;
//}
//The following is a transaction
file.copy (NewPath, "2011.xls", true);
if (file.exists (NewPath))
File.delete (NewPath);
}
catch (Exception e)
{
}
finally
{
Conn. Dispose ();
}
Thread.Sleep (1000);
}
}