ado.net excel 模版

ado.net excel 模版

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\";" +
                    // 指定扩展属性为 Microsoft Excel 8.0 (97) 9.0 (2000) 10.0 (2002),并且第一行作为数据返回,且以文本方式读取       
                    "data source=" + newPath;

                OleDbConnection conn = new OleDbConnection(connStr);
                OleDbCommand cmd = new OleDbCommand("", conn);

                try
                {
                    conn.Open();

                    string sql = "insert into [Sheet2$] (姓名,性别,联系电话) 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)
                    //{
                    //    //客户端还开着
                    //    byteToRead = reader.Read(buffer, 0, length);
                    //    length -= byteToRead;
                    //    //客户端断开时
                    //    //length = -1;
                    //}

                    //以下为一个事务
                    File.Copy(newPath, "2011.xls", true);
                    if (File.Exists(newPath))
                        File.Delete(newPath);


                }
                catch (Exception e)
                {
                   
                }
                finally
                {
                    conn.Dispose();
                }

                Thread.Sleep(1000);
            }
        }

 

ado.net excel 模版,古老的榕树,5-wow.com

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。