• 大小: 46KB
    文件类型: .rar
    金币: 1
    下载: 0 次
    发布日期: 2021-06-05
  • 语言: C#
  • 标签: C#  创建  Excel  导出  数据  

资源简介

C#创建Excel文件并将数据导出到Excel文件,实现效果及细节参考: http://blog.csdn.net/testcs_dn/article/details/25509249

资源截图

代码片段和文件信息

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.Odbc;
using System.Data.OleDb;
using System.IO;
using System.Security;
using System.Security.AccessControl;

namespace CSharp导出Excel文件
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender EventArgs e)
        {
            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;
            }

            //给Excel文件添加“EveryoneUsers“用户组的完全控制权限
            FileInfo fi = new FileInfo(excelPath);
            System.Security.AccessControl.FileSecurity fileSecurity = fi.GetAccessControl();
            fileSecurity.AddAccessRule(new FileSystemAccessRule(“Everyone“ FileSystemRights.FullControl AccessControlType.Allow));
            fileSecurity.AddAccessRule(new FileSystemAccessRule(“Users“ FileSystemRights.FullControl AccessControlType.Allow));
            fi.SetAccessControl(fileSecurity);

            //给Excel文件所在目录添加“EveryoneUsers“用户组的完全控制权限
            DirectoryInfo di = new DirectoryInfo(Path.GetDirectoryName(excelPath));
            System.Security.AccessControl.DirectorySecurity dirSecurity = di.GetAccessControl();
            dirSecurity.AddAccessRule(new FileSystemAccessRule(“Everyone“ FileSystemRights.FullControl AccessControlType.Allow));
            dirSecurity.AddAccessRule(new FileSystemAccessRule(“Users“ FileSystemRights.FullControl AccessControlType.Allow));
            di.SetAccessControl(dirSecurity);

            //定义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;
            try
            {
                conn.Open();
                //在dataGridView1中

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----

     文件        911  2013-03-19 17:01  CSharp导出Excel文件.sln

    ..A..H.     22016  2014-05-11 00:00  CSharp导出Excel文件.suo

     文件        144  2014-05-10 13:49  CSharp导出Excel文件\app.config

     文件       1865  2014-05-10 22:36  CSharp导出Excel文件\bin\Debug\CSharp导出Excel文件.vshost.application

     文件      11600  2014-05-10 22:39  CSharp导出Excel文件\bin\Debug\CSharp导出Excel文件.vshost.exe

     文件        144  2014-05-10 13:49  CSharp导出Excel文件\bin\Debug\CSharp导出Excel文件.vshost.exe.config

     文件       4536  2014-05-10 22:36  CSharp导出Excel文件\bin\Debug\CSharp导出Excel文件.vshost.exe.manifest

     文件      10351  2014-05-10 15:05  CSharp导出Excel文件\bin\Debug\Excel635353311441737186.xlsx

     文件       5768  2014-05-10 13:51  CSharp导出Excel文件\CSharp导出Excel文件.csproj

     文件        523  2014-05-10 13:51  CSharp导出Excel文件\CSharp导出Excel文件.csproj.user

     文件       6766  2014-05-10 23:19  CSharp导出Excel文件\Form1.cs

     文件       5062  2014-05-09 22:59  CSharp导出Excel文件\Form1.Designer.cs

     文件       5817  2014-05-09 22:59  CSharp导出Excel文件\Form1.resx

     文件        946  2014-05-10 23:59  CSharp导出Excel文件\obj\x86\Debug\CSharp导出Excel文件.csproj.FileListAbsolute.txt

     文件       7534  2014-05-10 13:22  CSharp导出Excel文件\obj\x86\Debug\DesignTimeResolveAssemblyReferences.cache

     文件       6401  2014-05-10 22:36  CSharp导出Excel文件\obj\x86\Debug\DesignTimeResolveAssemblyReferencesInput.cache

     文件       4608  2014-05-10 13:49  CSharp导出Excel文件\obj\x86\Debug\TempPE\Properties.Resources.Designer.cs.dll

     文件        504  2013-03-19 17:01  CSharp导出Excel文件\Program.cs

     文件       2494  2014-05-10 13:53  CSharp导出Excel文件\Properties\app.manifest

     文件       1396  2013-03-19 17:01  CSharp导出Excel文件\Properties\AssemblyInfo.cs

     文件       3104  2014-05-10 13:49  CSharp导出Excel文件\Properties\Resources.Designer.cs

     文件       6183  2013-03-19 17:08  CSharp导出Excel文件\Properties\Resources.resx

     文件       1122  2014-05-10 13:49  CSharp导出Excel文件\Properties\Settings.Designer.cs

     文件        249  2013-03-19 17:01  CSharp导出Excel文件\Properties\Settings.settings

     文件      10351  2014-05-10 13:24  CSharp导出Excel文件\Resource\Excel.xlsx

     目录          0  2014-05-10 13:49  CSharp导出Excel文件\obj\x86\Debug\TempPE

     目录          0  2014-05-10 23:59  CSharp导出Excel文件\obj\x86\Debug

     目录          0  2014-05-10 20:17  obj\x86\Debug

     目录          0  2014-05-10 23:59  CSharp导出Excel文件\bin\Debug

     目录          0  2013-03-19 17:01  CSharp导出Excel文件\obj\x86

............此处省略10个文件信息

评论

共有 条评论