• 大小: 708KB
    文件类型: .rar
    金币: 2
    下载: 1 次
    发布日期: 2021-06-19
  • 语言: C#
  • 标签: C#  导入exce  

资源简介

C# 导入excel到datagridview 在项目引用NuGet中安装Microsoft.Office.Interop.Excel

资源截图

代码片段和文件信息

using System;
using System.Linq;
using System.Windows.Forms;
using System.Reflection;
using Excel = Microsoft.Office.Interop.Excel;
using System.Diagnostics;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender EventArgs e)
        {
            //选择文件
            openFileDialog1.Filter = “XLS文件|*.xls|XLSX文件|*.xlsx“;//筛选文件类型
            openFileDialog1.FileName = ““;
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                OpenExcel(openFileDialog1.FileName);
            }
            openFileDialog1.Dispose();
        }

        private void OpenExcel(string strFileName)
        {
            object missing = Missing.Value;
            Excel.Application excel = new Excel.Application();//启动excel程序
            try
            {
                if (excel == null)
                {
                    MessageBox.Show(“无法访问Excel程序,请重新安装Microsoft Office Excel。“);
                }
                else
                {
                    excel.Visible = false;//设置调用引用的Excel文件是否可见
                    excel.UserControl = true;//设置调用引用的Excel是由用户创建或打开的
                    // 以只读的形式打开EXCEL文件(工作簿)想了解这堆参数请访问https://msdn.microsoft.com/zh-cn/library/office/microsoft.office.interop.excel.workbooks.open.aspx
                    Excel.Workbook wb = excel.Application.Workbooks.Open(strFileName missing true missing missing missing
                     missing missing missing true missing missing missing missing missing);
                    //取得第一个工作表
                    Excel.Worksheet ws = (Excel.Worksheet)wb.Worksheets[1];//索引从1开始 
                    //取得总记录行数(包括标题列)  
                    int rowCount = ws.UsedRange.Cells.Rows.Count; //得到行数 
                    int colCount = ws.UsedRange.Cells.Columns.Count;//得到列数
                    //初始化datagridview1
                    dataGridView1.Rows.Clear();
                    dataGridView1.Columns.Clear();
                    //取得第一行,生成datagridview标题列(下标是从1开始的)
                    for (int i = 1; i <= colCount; i++)
                    {
                        if (ws.Cells[1 i].Value2 == null)
                        { break; }
                        string cellStr = ws.Cells[1 i].Value2.ToString().Trim();
                        dataGridView1.Columns.Add(“column“ + i cellStr);
                    }
                    //取得数据(不包括标题列)
                    for (int i = 2; i <= rowCount; i++)
                    {//循环行
                        int index = dataGridView1.Rows.Add();
                        if (ws.Rows[i] != null)
                        {
                            for (int j = 1; j <= colCount; j++)
                            {//循环列
                                if (ws.Cells[i j].Value2 == null)
                         

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

    ..A..H.     44544  2019-04-11 14:46  WindowsFormsApplication1\.vs\WindowsFormsApplication1\v14\.suo

     文件       9475  2018-10-18 23:49  WindowsFormsApplication1\packages\Microsoft.Office.Interop.Excel.15.0.4795.1000\.signature.p7s

     文件    1638128  2016-03-10 16:14  WindowsFormsApplication1\packages\Microsoft.Office.Interop.Excel.15.0.4795.1000\lib\net20\Microsoft.Office.Interop.Excel.dll

     文件     449414  2019-04-11 14:45  WindowsFormsApplication1\packages\Microsoft.Office.Interop.Excel.15.0.4795.1000\Microsoft.Office.Interop.Excel.15.0.4795.1000.nupkg

     文件        189  2019-04-11 14:42  WindowsFormsApplication1\WindowsFormsApplication1\App.config

     文件       9014  2019-04-11 12:09  WindowsFormsApplication1\WindowsFormsApplication1\bin\Debug\123456.xls

     文件      17920  2019-04-11 14:46  WindowsFormsApplication1\WindowsFormsApplication1\bin\Debug\WindowsFormsApplication1.exe

     文件        189  2019-04-11 14:42  WindowsFormsApplication1\WindowsFormsApplication1\bin\Debug\WindowsFormsApplication1.exe.config

     文件      22016  2019-04-11 14:46  WindowsFormsApplication1\WindowsFormsApplication1\bin\Debug\WindowsFormsApplication1.pdb

     文件      22696  2019-04-11 14:46  WindowsFormsApplication1\WindowsFormsApplication1\bin\Debug\WindowsFormsApplication1.vshost.exe

     文件        189  2019-04-11 14:42  WindowsFormsApplication1\WindowsFormsApplication1\bin\Debug\WindowsFormsApplication1.vshost.exe.config

     文件        490  2018-04-12 07:35  WindowsFormsApplication1\WindowsFormsApplication1\bin\Debug\WindowsFormsApplication1.vshost.exe.manifest

     文件       4304  2019-04-11 14:46  WindowsFormsApplication1\WindowsFormsApplication1\Form1.cs

     文件       3203  2019-04-11 14:45  WindowsFormsApplication1\WindowsFormsApplication1\Form1.Designer.cs

     文件       6020  2019-04-11 14:45  WindowsFormsApplication1\WindowsFormsApplication1\Form1.resx

     文件        868  2019-04-11 14:42  WindowsFormsApplication1\WindowsFormsApplication1\obj\Debug\DesignTimeResolveAssemblyReferences.cache

     文件       7724  2019-04-11 14:45  WindowsFormsApplication1\WindowsFormsApplication1\obj\Debug\DesignTimeResolveAssemblyReferencesInput.cache

     文件          0  2019-04-11 14:42  WindowsFormsApplication1\WindowsFormsApplication1\obj\Debug\TemporaryGeneratedFile_036C0B5B-1481-4323-8D20-8F5ADCB23D92.cs

     文件          0  2019-04-11 14:42  WindowsFormsApplication1\WindowsFormsApplication1\obj\Debug\TemporaryGeneratedFile_5937a670-0e60-4077-877b-f7221da3dda1.cs

     文件          0  2019-04-11 14:42  WindowsFormsApplication1\WindowsFormsApplication1\obj\Debug\TemporaryGeneratedFile_E7A71F73-0F8D-4B9B-B56E-8E70B10BC5D3.cs

     文件       1401  2019-04-11 14:46  WindowsFormsApplication1\WindowsFormsApplication1\obj\Debug\WindowsFormsApplication1.csproj.FileListAbsolute.txt

     文件       1012  2019-04-11 14:46  WindowsFormsApplication1\WindowsFormsApplication1\obj\Debug\WindowsFormsApplication1.csproj.GenerateResource.Cache

     文件       5878  2019-04-11 14:46  WindowsFormsApplication1\WindowsFormsApplication1\obj\Debug\WindowsFormsApplication1.csprojResolveAssemblyReference.cache

     文件      17920  2019-04-11 14:46  WindowsFormsApplication1\WindowsFormsApplication1\obj\Debug\WindowsFormsApplication1.exe

     文件        180  2019-04-11 14:46  WindowsFormsApplication1\WindowsFormsApplication1\obj\Debug\WindowsFormsApplication1.Form1.resources

     文件      22016  2019-04-11 14:46  WindowsFormsApplication1\WindowsFormsApplication1\obj\Debug\WindowsFormsApplication1.pdb

     文件        180  2019-04-11 14:46  WindowsFormsApplication1\WindowsFormsApplication1\obj\Debug\WindowsFormsApplication1.Properties.Resources.resources

     文件        167  2019-04-11 14:45  WindowsFormsApplication1\WindowsFormsApplication1\packages.config

     文件        536  2019-04-11 14:42  WindowsFormsApplication1\WindowsFormsApplication1\Program.cs

     文件       1356  2019-04-11 14:42  WindowsFormsApplication1\WindowsFormsApplication1\Properties\AssemblyInfo.cs

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

评论

共有 条评论