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

推荐订阅源

Forbes - Security
Forbes - Security
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
L
LangChain Blog
量子位
GbyAI
GbyAI
B
Blog RSS Feed
月光博客
月光博客
人人都是产品经理
人人都是产品经理
腾讯CDC
Recent Announcements
Recent Announcements
Microsoft Azure Blog
Microsoft Azure Blog
I
InfoQ
The Cloudflare Blog
D
Docker
Cyberwarzone
Cyberwarzone
U
Unit 42
NISL@THU
NISL@THU
C
Check Point Blog
B
Blog
大猫的无限游戏
大猫的无限游戏
Cisco Talos Blog
Cisco Talos Blog
Recorded Future
Recorded Future
H
Hackread – Cybersecurity News, Data Breaches, AI and More
J
Java Code Geeks
G
GRAHAM CLULEY
Engineering at Meta
Engineering at Meta
酷 壳 – CoolShell
酷 壳 – CoolShell
博客园 - 叶小钗
P
Proofpoint News Feed
F
Fortinet All Blogs
V
V2EX
T
Threat Research - Cisco Blogs
T
Threatpost
S
SegmentFault 最新的问题
Know Your Adversary
Know Your Adversary
雷峰网
雷峰网
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
博客园 - 司徒正美
P
Privacy & Cybersecurity Law Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
TaoSecurity Blog
TaoSecurity Blog
Latest news
Latest news
Apple Machine Learning Research
Apple Machine Learning Research
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Y
Y Combinator Blog
P
Privacy International News Feed
L
Lohrmann on Cybersecurity
AWS News Blog
AWS News Blog
G
Google Developers Blog
美团技术团队

博客园 - 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
    }
}