2

所有,关于这个主题有很多问题,但没有一个能解决我的问题。我编写了一个相当复杂的例程来使用 OleDb 和 Access 数据库引擎将传递的DataSet/导出DataTable到 Excel(我在 Win7 下运行 Office 2010)。问题是无论我如何定义要写入 Excel 的列,所有值都导出为TEXT/STRING字段。

我正在使用OleDbConnection字符串

string fileName = @"F:\SomePath\MyExcel.xlsx";
string connectionString = String.Format(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=
    {0};Extended Properties=""Excel 12.0 Xml;HDR=YES;MaxScanRows=0;IMEX=0""", fileName);

并尝试了许多其他连接字符串选项,但没有成功。

我在代码中生成 Excel 定义,但明确地将这些定义生成为

CREATE TABLE [MetaTab] ([Table] TEXT,[Field] TEXT,[Seq] NUMBER,[DataLevel] NUMBER)

然后我为插入生成代码,对于上面的示例,这是

INSERT INTO [MetaTab$]([Table],[Field],[Seq],[DataLevel])VALUES('B1A','EstabID','1','9')

这可行,但所有值都写为 TEXT。如何强制 Excel 采用其他数据格式?

注意:我尝试删除非字符串的撇号,但这也不起作用。我真的被困住了,任何想法都将不胜感激。谢谢你的时间。

4

2 回答 2

1

在 Excel 工作表中插入值时,无法向 OleDbCommand 提供任何提示(语法)以插入数值。打开工作表时,它不会在工作表中将数值显示为数字。

有一种解决方法,当在 excel 文件中创建第一条记录时,然后转到第一条记录并在已存在记录的单元格(需要数字数据的地方)中重新输入相同的值。

或者

您可以在该 Excel 表中放置带有数值的默认记录。

下面提到了示例代码,用于在工作表中创建第一条记录后将数据类型转换为数字。当任何记录插入工作表时调用一次 ChangeFormat 函数,进一步的数据将以正确的格式保存。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Office.Interop.Excel;
using System.Data;

namespace ConsoleApplication3
{
    class Program
    {
        static void Main(string[] args)
        {


        System.Data.DataTable xlsData = new System.Data.DataTable();
        string xlsConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=d:\\romil.xlsx;Extended Properties=\"Excel 12.0;HDR=Yes;\"";
        System.Data.OleDb.OleDbConnection xlsCon = new System.Data.OleDb.OleDbConnection(xlsConnectionString);
        System.Data.OleDb.OleDbCommand xlsCommand;
        int recUpdate;
        int recordsinSheet;

        xlsCommand = new System.Data.OleDb.OleDbCommand("Select count(*) as RecCount from [Sheet1$]");
        xlsCommand.Connection = xlsCon;
        xlsCon.Open();
        recordsinSheet =Convert.ToInt32( xlsCommand.ExecuteScalar());

        xlsCommand=   new System.Data.OleDb.OleDbCommand("Insert into [Sheet1$] (Field1,Field2) values ('123',2)");
        xlsCommand.Connection = xlsCon;

        recUpdate = xlsCommand.ExecuteNonQuery();
        xlsCon.Close();

        if ((recordsinSheet + recUpdate) == 1)
            ChangeFormat();

        Console.ReadKey();

    }

    private static void ChangeFormat()
    {
        string filename = "d:\\romil.xlsx";

        object missing = System.Reflection.Missing.Value ;

        Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();

        Microsoft.Office.Interop.Excel.Workbook wb = excel.Workbooks.Open(filename, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing);

        Microsoft.Office.Interop.Excel.Worksheet wsh=null;
        foreach (Microsoft.Office.Interop.Excel.Worksheet sheet in wb.Sheets)
        {
            if (sheet.Name == "Sheet1")
            {
                wsh = sheet;
                break;
            }
        }

        for (int rCnt = 2; rCnt <= wsh.Rows.Count; rCnt++)
        {

            if  ( wsh.Cells[rCnt, 2].Value== null)
                break;

            wsh.Cells[rCnt, 2] = wsh.Cells[rCnt, 2].Value;
        }    

        wb.SaveAs(filename, missing,
            missing, missing, missing, missing,
           Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange,
            missing, missing, missing,
            missing, missing);
        wb.Close();
    }
}

}

于 2012-04-28T15:34:14.707 回答
1

我不认为你可以。excel 格式实际上并不类似于数据库列数据类型 - 最终基础值始终是数字或字符串;然后格式决定它的显示方式。

即使我错了——我个人更喜欢使用http://epplus.codeplex.com/来生成我的 Excel 电子表格——你可以在其中做很多非常高级的东西,也可以做一些简单的东西,比如格式化。

于 2012-04-27T10:51:23.100 回答