通过没有标题的OLEDB创建Excel工作簿 [英] Creating an Excel Workbook via OLEDB without headers

查看:156
本文介绍了通过没有标题的OLEDB创建Excel工作簿的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个C#程序可以写入Excel,该程序支持不带标题的文件和带有该文件的文件.

I have a C# program to write to Excel which is to support files without headers and files with.

如果我在需要标题的地方写,这很好,但是在读取没有标题的文件的情况下,我想写一个没有标题的表.如果我使用相同的标题来创建表,则内容中仍会显示F1,F2,F3等.

If I write where I want headers, this is fine, but in the case of reading a file without headers, I then want to write a table without headers. If I use the same headings to create a table, I still get F1, F2, F3 etc in the content.

我写的连接字符串是:-

My connection string for writing is:-

Provider = Microsoft.Jet.OLEDB.4.0;数据源= 文件名;扩展属性="Excel 8.0; HDR =否; IMEX = 0;"

Provider=Microsoft.Jet.OLEDB.4.0; Data Source=filename;Extended Properties="Excel 8.0;HDR=No;IMEX=0;"

关于如何丢失标题的任何想法?

Any ideas on how to lose the headers?

推荐答案

这个问题现在有点老了,但是下面是我如何回答另一个类似的问题:

This question's a bit old now, but the following is how I answered another similar question:

这不是很漂亮,但这是我发现创建一个没有任何内容的新工作表的唯一方法.我发现的唯一问题是,Oledb会在CREATE命令中指定的标头单元格上自动创建一个命名范围,但是假设您对此并不关心,则应该可以正常工作.

This isn't pretty, but it's the only way I've found to create a new worksheet with nothing in it. The only problem I've discovered is that Oledb automatically creates a named range on the header cells specified in the CREATE command, but assuming you don't care about that then this should work fine.

string connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + fileName +
    ";Mode=ReadWrite;Extended Properties=\"Excel 8.0;HDR=NO\"";

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

    using (OleDbCommand cmd = new OleDbCommand())
    {
        cmd.Connection = conn;
        cmd.CommandText = "CREATE TABLE [MySheet] (a string)";  // Doesn't matter what the field is called
        cmd.ExecuteNonQuery();

        cmd.CommandText = "UPDATE [MySheet$] SET F1 = \"\"";
        cmd.ExecuteNonQuery();
    }

    conn.Close();
}

这篇关于通过没有标题的OLEDB创建Excel工作簿的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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