• 使用Microsoft.Office.Interop.Excel提取Excel文件数据


    将Excel数据提取出来放到二维数组中:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Data;
    using System.Data.OleDb;
    using Microsoft.Office.Interop.Excel;
    
    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                DataSet ds = ExcelToDataTable(@"c:aaa.xls", "aaa");
    
                System.Data.DataTable ExcelTable = ds.Tables["aaa"];
    
                int iColums = ExcelTable.Columns.Count;//列数
    
                int iRows = ExcelTable.Rows.Count;//行数
    
                string[,] storedata = new string[iRows, iColums];
                for (int i = 0; i < ExcelTable.Rows.Count; i++)
                    for (int j = 0; j < ExcelTable.Columns.Count; j++)
                    {
                        storedata[i, j] = ExcelTable.Rows[i][j].ToString();
                    }
    
                string strSql = GetStrSql(storedata, iRows);
    
            }
    
            public static DataSet ExcelToDataTable(string strExcelFileName, string strSheetName)
            {
    
                string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + strExcelFileName + ";" + "Extended Properties=Excel 5.0;";
    
                string strExcel = string.Format("select * from [{0}$]", strSheetName);
    
                DataSet ds = new DataSet();
    
                using (OleDbConnection conn = new OleDbConnection(strConn))
                {
                    Application app = new Application();
                    app.Application.Workbooks.Open(strExcelFileName);
                    //app.Visible = true;
                    conn.Open();
    
                    OleDbDataAdapter adapter = new OleDbDataAdapter(strExcel, strConn);
    
                    adapter.Fill(ds, strSheetName);
    
                    conn.Close();
    
                }
                return ds;
            }
    
            public static string GetStrSql(string[,] datainfo, int rows)
            {
                string strSql = "";
                for (int i = 0; i < datainfo.GetLength(0); i++)
                {
    
                    strSql = "insert into tmp_localfile_data(finish_time,data_value,product_id,prod_group,proj_code,parameter_id," +
                             " machine,category_flag,partno,fixture,marking,head_id,shift,batch,id_1,id_2)" +
                             " VALUES(to_date('" + datainfo[i, 0] + "','mm/dd/yyyy hh24:mi:ss'),'" + datainfo[i, 1] +
                             "','" + datainfo[i, 2] + "','" + datainfo[i, 3] + "','" + datainfo[i, 4] + "'," +
                             "'" + datainfo[i, 5] + "','" + datainfo[i, 6] + "','" + datainfo[i, 7] + "','" + datainfo[i, 8] +
                             "','" + datainfo[i, 9] + "','" + datainfo[i, 10] + "','" + datainfo[i, 11] + "','" + datainfo[i, 12] +
                             "','" + datainfo[i, 13] + "','" + datainfo[i, 14] + "','" + datainfo[i, 15] + "')";
                }
    
                return strSql;
            }
    
        }
    }
  • 相关阅读:
    HarmonyOS Java UI之AdaptiveBoxLayout布局示例
    【鸿蒙开发板试用报告】OneNet平台+开发板实时监控温湿度(一)
    安装了瑞友天翼4.0后出现了远程桌面无法连接的问题
    CISVC.EXE的资源占用
    Delphi如何在窗体标题栏添加按钮
    Delphi中捕捉窗体的最小化、最大化、还原消息
    打印机任务无法删除
    Delphi创建一个虚幻的层窗口(Win2000/XP)
    工资年结时提示“上年数据已经结转”
    Delphi中如何控制其他程序窗体上的窗口控件
  • 原文地址:https://www.cnblogs.com/YuanDong1314/p/8967905.html
Copyright © 2020-2023  润新知