码迷,mamicode.com
首页 > Web开发 > 详细

ado.net excel 模版

时间:2014-06-13 18:52:20      阅读:309      评论:0      收藏:0      [点我收藏+]

标签:style   ext   color   com   strong   数据   

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 模版,布布扣,bubuko.com

ado.net excel 模版

标签:style   ext   color   com   strong   数据   

原文地址:http://www.cnblogs.com/chengzihu/p/3784497.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!