C#创建Excel文件并将数据导出到Excel文件的示例代码详解(图)


当前第2页 返回上一页


从资源中提取Excel文件

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

string excelPath = AppDomain.CurrentDomain.BaseDirectory + "Excel" + DateTime.Now.Ticks + ".xlsx";

if (System.IO.File.Exists(excelPath))

{

    textBox1.Text += ("文件已经存在!");

    return;

}

 

try

{

    //从资源中提取Excel文件

    System.IO.FileStream fs = new System.IO.FileStream(excelPath, FileMode.OpenOrCreate);

    fs.SetLength(0);

    fs.Write(Properties.Resources.Excel, 0, Properties.Resources.Excel.Length);

    fs.Close();

    fs.Dispose();

    textBox1.Text = "提取Excel文件成功!" + "\r\n";

}

catch (System.Exception ex)

{

    excelPath = string.Empty;

    textBox1.Text += ("提取Excel文件失败:" + ex.Message);

    textBox1.Text += ("\r\n");

    Application.DoEvents();

    return;

}

定义连接字符串

1

2

3

4

5

//定义OleDB连接字符串

            string strConn = "Provider=Microsoft.Ace.OleDb.12.0;Persist Security Info=False;" + "data source=" + @excelPath + ";

            Extended Properties='Excel 12.0; HDR=yes; IMEX=10'";

            OleDbConnection conn = new OleDbConnection();

            conn.ConnectionString = strConn;

注意:连接字符串中IMEX的值使用的是10,如果是1或2,在执行Insert Into语句时就会报“操作必须使用一个可更新的查询”的错误。

在dataGridView1中显示Excel文件中的所有表的信息

1

2

3

DataTable oleDt = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);

dataGridView1.DataSource = oleDt;

dataGridView1.Show();

向"Sheet1"表中插入几条数据,访问Excel的表的时候需要在表名后添加"$"符号,Insert语句可以不指定列名

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

OleDbCommand cmd = null;

try

{

    //向"Sheet1"表中插入几条数据,访问Excel的表的时候需要在表名后添加"$"符号,Insert语句可以不指定列名

    cmd = new OleDbCommand("Insert Into [Sheet1$] Values('abc', 'bac', '0', '123456', 'test','测试','aa')", conn);//(A,B,C,D,E,F,G)

    cmd.ExecuteNonQuery();

    cmd.ExecuteNonQuery();

    cmd.ExecuteNonQuery();

    cmd.ExecuteNonQuery();

    cmd.ExecuteNonQuery();

}

catch (System.Exception ex)

{

    textBox1.Text += ("插入数据失败:" + ex.Message);

    textBox1.Text += ("\r\n");

}

在dataGridView2中显示表"Sheet1"的内容,访问Excel的表的时候需要在表名后添加"$"符号

1

2

3

4

5

cmd = new OleDbCommand("Select * From [Sheet1$]", conn);

OleDbDataAdapter adp = new OleDbDataAdapter(cmd);

DataSet ds = new DataSet();

adp.Fill(ds);

dataGridView2.DataSource = ds.Tables[0];

遍历Schema的内容

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

DataTable dt = conn.GetSchema();

for (int i = 0; i < dt.Columns.Count; i++)

{

    textBox1.Text += dt.Columns[i].Caption;

    if (i + 1 < dt.Columns.Count)

    {

        textBox1.Text += ",";

    }

}

 

for (int j = 0; j < dt.Rows.Count; j++)

{

    for (int i = 0; i < dt.Columns.Count; i++)

    {

        if (dt.Rows[j][dt.Columns[i]] != null)

        {

            textBox1.Text += dt.Rows[j][dt.Columns[i]].ToString();

        }

        else

        {

            textBox1.Text += "null";

        }

 

        if (i + 1 < dt.Columns.Count)

        {

            textBox1.Text += ",";

        }

    }

    textBox1.Text += ("\r\n");

}

关闭Excel数据连接

1

2

3

4

5

6

7

8

9

10

11

12

if (conn.State != ConnectionState.Closed)

{

    try

    {

        conn.Close();

    }

    catch (System.Exception ex)

    {

        textBox1.Text += ("关闭Excel数据连接:" + ex.Message);

        textBox1.Text += ("\r\n");

    }

}

打开文件目录

System.Diagnostics.Process.Start("explorer.exe", AppDomain.CurrentDomain.BaseDirectory);

以上就是C#创建Excel文件并将数据导出到Excel文件的示例代码详解(图)的详细内容!

返回前面的内容

相关阅读 >>

.net的优点

C# 应用npoi获取excel中的图片,保存至本地的算法的图文代码实例详解

使用C#实现发送自定义的html格式邮件的代码案例

C#注册控件处理程序(setconsolectrlhandler)函数示例的详细介绍

C#二进制字节流查找函数indexof的示例代码详解

asp.net(C#)如何读取excel的文件的实例详解

详解C#常用正则验证函数的示例代码

C#生成一万以内所有不重复数字的四位数

C#学习之面向对象如何调用类以及普通方法、静态方法的使用

C# winform制作不规则窗体(代码)

更多相关阅读请进入《C#》频道 >>




打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...