简体   繁体   English

我怎样才能创建schema.ini文件?我需要将.csv文件导出到datagridview

[英]How can I create schema.ini file? I need to export my .csv file to datagridview

I want to export a CSV file to a datagridview. 我想将CSV文件导出到datagridview。 I need to create the file schema.ini . 我需要创建文件schema.ini But I don't know, how can I create it? 但我不知道,我怎么能创造它呢?

There is my code: 有我的代码:

    public DataTable exceldenAl(string excelFile)
    {
        try
        {
            string fileName = Path.GetFileName(excelFile);
            string pathOnly = Path.GetDirectoryName(excelFile);
            string cmd = "Select * From [" + fileName + "$]";

            string cnstr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + pathOnly + "\\;Extended Properties=\"Text;HDR=Yes;FORMAT=Delimited\"";
            OleDbConnection ocn = new OleDbConnection(cnstr);
            ocn.Open();
            OleDbCommand command = new OleDbCommand(cmd,ocn);
            OleDbDataAdapter adap = new OleDbDataAdapter(command);

            DataTable dt = new DataTable();
            dt.Locale = CultureInfo.CurrentCulture;
            adap.Fill(dt);
            return dt;
        }
        finally {
        }
    }


    private void btnExcelReader_Click(object sender, EventArgs e)
    {
        string dosya;
        string cevap;
        openFileDialog1.ShowDialog();
        dosya = openFileDialog1.FileName.ToString();
        ClsExcelReader er = new ClsExcelReader();
        cevap = er.exceldenAl(dosya).ToString();
        dataGridView1.DataSource = cevap;
        //listViewExcelOku.DataBindings =
    }
}

Open up notepad and create a file similar to this: 打开记事本并创建一个类似于此的文件:

[YourCSVFileName.csv]
ColNameHeader=True
Format=CSVDelimited
DateTimeFormat=dd-MMM-yyyy
Col1=A DateTime
Col2=B Text Width 100
Col3=C Text Width 100
Col4=D Long
Col5=E Double

Modify the above file to fit your specific data schema. 修改上述文件以适合您的特定数据模式。 Save it as SCHEMA.ini in the same directory where your *.CSV file is located. 将其作为SCHEMA.ini保存在* .CSV文件所在的同一目录中。

Read this link ( Importing CSV File Into Database ), it is a good example to get you up and understanding how the Schema.ini works 阅读此链接( 将CSV文件导入数据库 ),这是一个很好的例子,让您了解Schema.ini的工作原理

I wrote these Excel formulas to generate the content of this file if you can get hold of the Excel sheet with column headers. 如果您可以获取带有列标题的Excel工作表,我编写了这些Excel公式来生成此文件的内容。 It's pretty basic. 这很基础。 Add and remove features as desired. 根据需要添加和删除功能。 It assumes all text with delimiters. 它假设所有文本都带有分隔符。 Then insert the following formula (including all your options) in A2: 然后在A2中插入以下公式(包括所有选项):

="[no headers.csv]
"&"ColNameHeader=false
"&"MaxScanRows=0
"&"Format=Delimited(;)
Col"&COLUMN()&"="&A1&" text
"

And the following formula in B2. B2中的以下公式。

=A2&"Col"&COLUMN()&"="&B1&" text
"

Then drag to the right to get your basic schema.ini (rightmost cell). 然后向右拖动以获取基本schema.ini(最右边的单元格)。 You can adjust options in the excel cell below the column name. 您可以调整列名称下方的Excel单元格中的选项。 Each column has it's own definition. 每列都有自己的定义。 I got closing and opening quotes in the result when copied to text file. 复制到文本文件时,我在结果中关闭并打开引号。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用特殊字符从schema.ini构建DataTable模式 - How can I build a DataTable schema from schema.ini with special characters 使用schema.ini的C#import CSV文件创建表但不加载数据 - C# import CSV file with schema.ini creates table but doesn't load data 如何将DataGridView导出到csv文件? - How to export my DataGridView to a csv file? 如何将.ini文件放入datagridview中 - How do i put an .ini file in a datagridview 将 csv 文件导入 datagrid 后,我需要向 datagridview 添加或插入新记录,然后将最新数据导出到 csv 文件 - After import csv file to datagrid i need to add or insert new records to datagridview then export lastest data to csv file 使用“ Microsoft文本驱动程序”是否需要使用schema.ini文件? - Does using the “Microsoft Text Driver” require the use of a schema.ini file? 单个 schema.ini 定义可以覆盖多个文件吗 - Can a single schema.ini definition cover multiple files 如何将SQL Server结果集导出到CSV文件? - How can I export SQL Server result set into CSV file? 我可以在资源文件中存储 Ini 文件吗? - Can I store an Ini file in a Resources file? 在Visual Studio中,如何在用CSV文件填充的datagridView中搜索数据? - In Visual Studio, how can I search for data in a datagridView that was populated with a CSV file?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM