将Excel文件导入到delphi上的datagrid [英] importing an excel file to datagrid on delphi

查看:142
本文介绍了将Excel文件导入到delphi上的datagrid的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在.NET上尝试过此代码-导入一个Excel文件以devxpress datagrid-并正常工作,但是我现在必须创建一个在delphi上执行相同工作的过程.我对Delphi不太了解,所以我需要一些如何做的想法.

I've tried this code on .NET -to import an excel file to devxpress datagrid- and it worked fine but i have to create a procedure doing the same job on delphi now. i dont know much about delphi so i need some ideas how to do that.

   public static DataTable ImportExcelXLS(string FileName, bool hasHeaders)
    {
        string HDR = hasHeaders ? "Yes" : "No";
        string strConn;
        if (FileName.Substring(FileName.LastIndexOf('.')).ToLower() == ".xlsx")
            strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + FileName + ";Extended Properties=\"Excel 12.0;HDR=" + HDR + ";IMEX=0\"";
        else
            strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + FileName + ";Extended Properties=\"Excel 8.0;HDR=" + HDR + ";IMEX=0\"";

        //DataSet output = new DataSet();

        using (OleDbConnection conn = new OleDbConnection(strConn))
        {
            conn.Open();

                DataTable schemaTable = conn.GetOleDbSchemaTable(
                OleDbSchemaGuid.Tables, new object[] { null, null, null, "TABLE" });

            foreach (DataRow schemaRow in schemaTable.Rows)
            {
                string sheet = schemaRow["TABLE_NAME"].ToString();

                if (!sheet.EndsWith("_"))
                {
                    try
                    {

                        OleDbCommand cmd = new OleDbCommand("SELECT * FROM [" + sheet + "]", conn);
                        cmd.CommandType = CommandType.Text;

                        DataTable outputTable = new DataTable(sheet);

                        new OleDbDataAdapter(cmd).Fill(outputTable);

                        if (outputTable.Rows.Count > 0)
                        {
                            return outputTable;
                        }

                    }
                    catch (Exception ex)
                    {
                        throw new Exception(ex.Message + string.Format("Sheet:{0}.File:F{1}", sheet, FileName), ex);
                    }
                }
            }
        }
        return null;
    } 

推荐答案

您可以使用三个主要选项与Microsoft Excel表进行互操作

You have three main options to interop with Microsoft Excel tables

1)您的来源使用OLE DB来访问数据.这有三个主要的缺点:

1) you source uses OLE DB to reach data. This has three main misfeatures:

  • 不赞成使用Microsoft ADO,而Delphi ADO库则存在很多错误.这些错误具有解决方法,但是您将花费时间检测它们并学习这些技巧.
  • 这需要安装Microsoft Excel,并且不是免费程序
  • 电子表格不是表格数据,因此您只能以这种方式读取大多数简单的结构化数据.

但是,如果可以的话,它既快速又方便.

But it is fast and convenient, if it works.

  • ADO with XLSX files in Delphi XE
  • Update cell query for Excel ADO from Delphi
  • ADO SQL type cast Float to String (Excel)
  • Using ADODataset component to open an Excel Spreadsheet
  • Delphi ADO with Excel 2010 or newer, problems with .RecordCount
  • How to insert value into the Excel file using ADO?
  • Delphi ADO Query
  • Delphi - Excel rows get by an ADO Query
  • Provider selection, when trying to access Excel table in Delphi 7

您还可以阅读很多教程和操作方法.例如,使用BabelFish或GoogleTranslate,您可以尝试 http://devdelphi.ru/?p=63

You can also read a lot of tutorials and how-tos. For example, using BabelFish or GoogleTranslate you may try http://devdelphi.ru/?p=63

2)您可以将正在运行的Excel应用程序用作COM服务器.您只需要在表单上放置ExcelApplication组件.参见c:\ RAD Studio \ 9.0 \ OCX \ Servers \

2) You can use running Excel application as COM server. You just need to drop ExcelApplication component on your form. See c:\RAD Studio\9.0\OCX\Servers\

可以说这是使用Excel的最常用方法.它有几个优点:

That is arguably the most usual way to use Excel. It has few advantages:

  • COM接口是Excel的自然API,因此您可以访问其所有功能
  • 您可以在Visual Basic或C ++中获取任何代码示例,并将其逐字翻译为Delphi
  • Microsoft有很多关于该API的文档
  • 对于大多数操作,您可以使用内置的Excel宏记录器获取现成的程序,以及如何在Excel中执行操作
  • 通常,Excel是某些文件是否有效的最终授权.因此,您可以确保您的程序将能够读取给定的任何文件.如果没有,您可以责怪Microsoft Excel,而不是您的程序.
  • 与掌握什么是SQL以及如何使用它相比,在行上然后在单元格上进行循环可以说容易得多.

但是

  • 这需要安装Microsoft Excel,并且不是免费程序
  • 这很慢.甚至使用数组进行数据传输
  • 对于非英语语言环境,COM API中存在未修复的错误
  • Delphi COM实现中也存在一些错误.

同样,您有很多关于使用该组件的教程.而且您有很多有关使用Visual Basic控制Excel的Microsoft教程

Again, you have a lot of tutorials about using that component. And you have a lot of Microsoft tutorials about using Visual Basic to control Excel

  • International date formatting issues in OLE Excel output
  • https://www.google.ru/search?client=opera&q=site:stackoverflow.com+delphi+excelapplication&sourceid=opera

等等.如果您想更改此方法-您将获得很多信息

and so one. If you would like to change your approach to this one - you would have a LOT of info

2.1)也可以使用OpenOffice.org(运行scalc.exe服务器,通过COM或HTTP控制,就像您运行excel.exe服务器一样)来读取/写入Microsoft Office文件,某些Web服务器也可以这样做.但是,有可能我没有看到这种方法流行.因此,只需提及它即可.

2.1) One can also use OpenOffice.org (run scalc.exe server, controlled via COM or HTTP, just like you do run excel.exe server) to read/write Microsoft Office files and some web-servers do it. But whlie it is possible i did not saw that approach popular. So just mentioning it.

3)有些Delphi原生库能够直接读取/写入Excel文件

3) there are some Delphi-native libraries able to read/write Excel files directly

  • 这可能是最快的方法,所以当您拥有大量数据时,它会震撼
  • 您可以控制发生的事情-只要更改您自己的程序,您就可以随时更改库
  • 您的程序成为独立的-既不需要安装Excel本身,也不需要安装Excel OLE DB驱动程序.

但是

  • 这些库总是赶超Excel,因此它们仅支持其功能的子集,并且通常仅是一个狭窄的库.
  • 甚至文件格式:某些库仅支持XLS,有些仅支持XML和XLSX.尤其便宜或免费.
  • 这些库,就像任何程序一样,都有其错误.而且您将无法说这就是Excel的工作方式."

这篇关于将Excel文件导入到delphi上的datagrid的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆