惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
美团技术团队
腾讯CDC
V
V2EX
G
Google Developers Blog
博客园 - Franky
博客园 - 司徒正美
Stack Overflow Blog
Stack Overflow Blog
阮一峰的网络日志
阮一峰的网络日志
Microsoft Azure Blog
Microsoft Azure Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
T
The Blog of Author Tim Ferriss
Recent Announcements
Recent Announcements
Google DeepMind News
Google DeepMind News
罗磊的独立博客
C
Check Point Blog
MyScale Blog
MyScale Blog
aimingoo的专栏
aimingoo的专栏
F
Fortinet All Blogs
酷 壳 – CoolShell
酷 壳 – CoolShell
V2EX - 技术
V2EX - 技术
The Last Watchdog
The Last Watchdog
www.infosecurity-magazine.com
www.infosecurity-magazine.com
有赞技术团队
有赞技术团队
Security Archives - TechRepublic
Security Archives - TechRepublic
S
Security @ Cisco Blogs
W
WeLiveSecurity
D
DataBreaches.Net
Forbes - Security
Forbes - Security
V
Visual Studio Blog
P
Proofpoint News Feed
S
Secure Thoughts
H
Help Net Security
Cloudbric
Cloudbric
云风的 BLOG
云风的 BLOG
Microsoft Security Blog
Microsoft Security Blog
N
News and Events Feed by Topic
Schneier on Security
Schneier on Security
Engineering at Meta
Engineering at Meta
Attack and Defense Labs
Attack and Defense Labs
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
S
Security Affairs
L
LangChain Blog
Last Week in AI
Last Week in AI
Martin Fowler
Martin Fowler
Hacker News: Ask HN
Hacker News: Ask HN
H
Heimdal Security Blog
M
MIT News - Artificial intelligence
The Register - Security
The Register - Security

博客园 - Caviare

前台优化法则(YSlow) ASP.NET MVC源代码 启用IIS的Gzip压缩功能 web.sitemap Dynamic Generation SQL字符正则匹配 委托应用 WorkBook操作实例 判断大陆IPAddress 迭代器和排序基本使用 Bertrand Meyer提出的设计模式的原则 - Caviare - 博客园 Web.cofig详解[转] UpdatePanel Control [转老赵博客] .Net环境下的缓存技术介绍 (转) [转]webservice和remoting在分布式程序中的应用 sql2005备份在sql2000中恢复 温故知新-Excel操作类 SQL collation设置 一张类的访问权限图,帮您加深概念 [转]您可能不知道的Asp.Net2.0技巧
Excel WorkBook操作例
Caviare · 2007-08-03 · via 博客园 - Caviare

using System;
using System.Data;
using System.Drawing;
using System.Windows.Forms;
using Microsoft.VisualStudio.Tools.Applications.Runtime;
using Excel = Microsoft.Office.Interop.Excel;
using Office = Microsoft.Office.Core;
using System.Data.SqlClient;

namespace ExcelWorkbook1
{
    public partial class Sheet1
    {
        private void Sheet1_Startup(object sender, System.EventArgs e)
        {
        }

        private void Sheet1_Shutdown(object sender, System.EventArgs e)
        {
        }

        #region VSTO Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InternalStartup()
        {
            this.button1.Click += new System.EventHandler(this.button1_Click);
            this.Shutdown += new System.EventHandler(this.Sheet1_Shutdown);
            this.Startup += new System.EventHandler(this.Sheet1_Startup);

        }

        #endregion

        private void button1_Click(object sender, EventArgs e)
        {
            //import();
            export();
        }

        #region 数据库导入到xsl

        void import()
        {
            Excel.Application appXSL = new Microsoft.Office.Interop.Excel.Application();
            if (null == appXSL)
            {
                MessageBox.Show("Can Not Open Excel!");
                return;
            }
            SqlConnection sqlcon = new SqlConnection("server=.;uid=sa;pwd=;database=Article;");
            try
            {
                appXSL.Application.Workbooks.Add(true);
                sqlcon.Open();
                string sql = @"select * from articledetails";
                SqlCommand cmd = new SqlCommand(sql, sqlcon);
                SqlDataReader sdr = cmd.ExecuteReader();
                int rowCount = sdr.FieldCount;
                for (int i = 0; i < rowCount; i++)
                {
                    appXSL.Cells[1, i + 1] = sdr.GetName(i);
                }
                int currentRowNumber = 2;
                while (sdr.Read())
                {
                    for (int i = 0; i < rowCount; i++)
                    {
                        appXSL.Cells[currentRowNumber, i+1] = sdr.GetValue(i).ToString();
                    }
                    currentRowNumber++;
                }
                appXSL.Visible = true;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
            finally
            {
                if (sqlcon.State == ConnectionState.Open)
                {
                    sqlcon.Close();
                }
                appXSL.Quit();
            }
        }
        #endregion

        #region xsl导入到数据库
        void export()
        {
            Excel.Application appXSL = new Microsoft.Office.Interop.Excel.Application();
            try
            {
                Excel.Workbook workbook = appXSL.Workbooks.Open(@"d:\2.xls", System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing);
                Excel.Worksheet sheet = appXSL.Sheets[1] as Excel.Worksheet;
                System.Text.StringBuilder sbSql = new System.Text.StringBuilder();
                int rowCount = 5;
                int colCount = 8;
               
                string sqlTemp = @" insert into articledetails ( ";
                for (int i = 2; i <= colCount; i++)
                {
                    sqlTemp = sqlTemp + ((Excel.Range)sheet.Cells[1, i]).Text.ToString() + ",";
                }
                sqlTemp = sqlTemp.Substring(0, sqlTemp.Length - 1) + ") values (";

                for (int i = 2; i <= rowCount; i++)
                {
                    sbSql.Append(sqlTemp);
                    for (int j = 2; j < colCount; j++)
                    {
                        sbSql.Append("'");
                        sbSql.Append(((Excel.Range)sheet.Cells[i, j]).Text.ToString());
                        sbSql.Append("',");
                    }
                    sbSql.Append("'");
                    sbSql.Append(((Excel.Range)sheet.Cells[i, colCount]).Text.ToString() + "')");
                }
                openCon();
                SqlCommand cmd = new SqlCommand(sbSql.ToString(), sqlcon);
                cmd.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
            finally
            {
                MessageBox.Show("Export OK!");
                closeCon();
                appXSL.Quit();
                appXSL = null;
            }
        }
        #endregion

        #region 数据库
        SqlConnection sqlcon = new SqlConnection("server=.;uid=sa;pwd=;database=Article;");
        void openCon()
        {
            if (sqlcon.State == ConnectionState.Closed)
            {
                sqlcon.Open();
            }
        }
        void closeCon()
        {
            if (sqlcon.State == ConnectionState.Open)
            {
                sqlcon.Close();
            }
        }
        #endregion
    }
}