C#读取Excel文件(*.xls)|*.xls(2种方法)


1.


public static DataTable ImportExcel()
{
    XlsDocument document = null;
    OpenFileDialog dialog = new OpenFileDialog {
        Title = "导入Excel",
        Filter = "Excel文件(*.xls)|*.xls"
    };
    if (dialog.ShowDialog() == DialogResult.OK)
    {
        try
        {
            document = new XlsDocument(dialog.FileName);
        }
        catch
        {
        }
    }
    else
    {
        return null;
    }
    try
    {
        Worksheet worksheet = document.Workbook.Worksheets[0];
        ushort cellCount = worksheet.Rows[1].CellCount;
        DataTable table = new DataTable();
        int num2 = 1;
        while (num2 <= cellCount)
        {
            string columnName = worksheet.Rows[1].GetCell(ushort.Parse(num2.ToString())).Value.ToString();
            table.Columns.Add(columnName);
            num2++;
        }
        for (int i = 2; i < worksheet.Rows.Count; i++)
        {
            DataRow row = table.NewRow();
            for (num2 = 1; num2 <= cellCount; num2++)
            {
                string str2 = worksheet.Rows[ushort.Parse(i.ToString())].GetCell(ushort.Parse(num2.ToString())).Value.ToString();
                row[num2 - 1] = str2;
            }
            table.Rows.Add(row);
        }
        return table;
    }
    catch
    {
        return null;
    }
}

 

 
 1 0x1public static DataTable ImportExcel()
{
    XlsDocument document = null;
    OpenFileDialog dialog = new OpenFileDialog {
        Title = "导入Excel",
        Filter = "Excel文件(*.xls)|*.xls"
    };
    if (dialog.ShowDialog() == DialogResult.OK)
    {
        try
        {
            document = new XlsDocument(dialog.FileName);
        }
        catch
        {
        }
    }
    else
    {
        return null;
    }
    try
    {
        Worksheet worksheet = document.Workbook.Worksheets[0];
        ushort cellCount = worksheet.Rows[1].CellCount;
        DataTable table = new DataTable();
        int num2 = 1;
        while (num2 <= cellCount)
        {
            string columnName = worksheet.Rows[1].GetCell(ushort.Parse(num2.ToString())).Value.ToString();
            table.Columns.Add(columnName);
            num2++;
        }
        for (int i = 2; i < worksheet.Rows.Count; i++)
        {
            DataRow row = table.NewRow();
            for (num2 = 1; num2 <= cellCount; num2++)
            {
                string str2 = worksheet.Rows[ushort.Parse(i.ToString())].GetCell(ushort.Parse(num2.ToString())).Value.ToString();
                row[num2 - 1] = str2;
            }
            table.Rows.Add(row);
        }
        return table;
    }
    catch
    {
        return null;
    }
}

 

**

2.

**

  //获取字典内容
        private void simpleButton5_Click(object sender, EventArgs e)
        {
            OpenFileDialog openfile = new OpenFileDialog();
            openfile.Filter = "工作薄(*.xlsx)|*.xlsx|工作薄(*.xls)|*.xls|所有文件(*.*)|*.*";
            if (openfile.FilterIndex == 1 && openfile.ShowDialog() == DialogResult.OK)
            {
                ExcelToDS(openfile.FileName, "DEPT");
            }
        }


  public DataTable ExcelToDS(string path, string type)
        {
            //try
            //{
            string path2 = path;
            //string strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path2 + ";Extended Properties=Excel 12.0;"; 
            string strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path2 + ";Extended Properties='Excel 12.0; HDR=yes; IMEX=1'";
            //string strConn = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source = " + path + "; Extended Properties=Excel 8.0;";
            OleDbConnection conn = new OleDbConnection(strConn);
            conn.Open();
            string strExcel = "";
            OleDbDataAdapter myCommand = null;
            DataSet ds = null;

            if (type == "DEPT")
            {
                strExcel = @"select [编码],	[名称],	[说明],'0' as color from [CT08.00.002 科室代码$]";
            }
            else if (type == "YPJX")
            {
                strExcel = @"select [编码],	[名称],	[说明],'0' as color from [CV08.50.002药物剂型代码$]";
            }
            else if (type == "YYPL")
            {
                strExcel = @"select [编码],	[名称],	[说明],'0' as color from [用药频率$]";
            }
            else if (type == "YYFF")
            {
                strExcel = @"select [编码],	[名称],	[说明],'0' as color from [CV06.00.102用药途径代码$]";
            }
            else if (type == "YWLX")
            {
                strExcel = @"select [编码],	[名称],	[说明],'0' as color from [CV5301.06药物类型代码$]";
            }else if(type=="JJJC")
            {
                strExcel = @"select [编码],	[名称],	[说明],'0' as color from [CV5199.01检验检查类别代码$]";
            }

            myCommand = new OleDbDataAdapter(strExcel, strConn);
            DataTable table1 = new DataTable();
            ds = new DataSet();
            myCommand.Fill(table1);
           return  table1;
            }
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值