0

所以我有一个 excel 表和一个 mssql 表,这两个表正在使用 LINQ 连接到 excel 表第 0 列(第一个)和 mssql 列 textfield2。我在加入字母数字值时遇到的问题,这似乎不起作用。当值是数字时,它确实有效。

        var adapter = new OleDbDataAdapter("SELECT * FROM [Sheet1$]", excconnectionstring);
        var adapter2 = new SqlDataAdapter("SELECT * FROM Table1", sqlconnectionstring);

        var ds = new DataSet();
        adapter.Fill(ds, "excel");
        adapter2.Fill(ds, "sql");

        var excel = ds.Tables["excel"].AsEnumerable();
        var esqel = ds.Tables["sql"].AsEnumerable();

        var query = from exc in excel
                    from sql in esqel
                    where exc[0].ToString() == sql.Field<string>("textfield2")
                    select new
                    {
                        debnr = sql.Field<string>("debnr"),
                        bedrag = double.Parse(exc[5].ToString())/100,
                        description = DateTime.Parse(exc[7].ToString(), new CultureInfo("nl-NL")).ToString("MMM yyyy"),
                        text1 = exc[0].ToString(),
                        projectno = sql.Field<string>("textfield1"),
                        central = sql.Field<string>("CentralizationAccount").Trim()
                    };

编辑:当我在 Excel 工作表中排序值并在顶部显示字母数字值时,似乎字母数字确实有效。但后来我遇到的问题是它不适用于数值..

4

1 回答 1

0

正如其他人所说,问题是由于 OleDb 根据前几行推断列类型而发生的。使用 Interop TextToColumn 我将 excel 列数据类型更改为文本。这是我用来执行此操作的代码:

        Workbook workBook = _excelApp.Workbooks.Open(Directory.GetCurrentDirectory() + "\\" + thisFileName,
            0, false, 5, "", "", false, XlPlatform.xlWindows, "",
true, false, 0, true, false, false);


        object fieldInfo = new int[1, 2] { { 1, 2 } };
        Range _range = ((Worksheet)workBook.Worksheets.get_Item("Sheet1")).UsedRange.Columns[1, Type.Missing];

        _range.TextToColumns(
 _range, XlTextParsingType.xlDelimited,
 XlTextQualifier.xlTextQualifierNone, Type.Missing,
 Type.Missing, Type.Missing, Type.Missing,
 Type.Missing, Type.Missing,
 Type.Missing,
 fieldInfo,
 Type.Missing, Type.Missing);

        _excelApp.DisplayAlerts = false;
        _excelApp.ScreenUpdating = false;
        _excelApp.Visible = false;
        _excelApp.UserControl = false;
        _excelApp.Interactive = false;

        workBook.SaveAs(Directory.GetCurrentDirectory() + "\\" + thisFileName, Type.Missing, Type.Missing, Type.Missing,
        Type.Missing, Type.Missing, XlSaveAsAccessMode.xlNoChange,
        Type.Missing, Type.Missing, Type.Missing, Type.Missing,
        Type.Missing);
        workBook.Close(false, thisFileName, null);
        Marshal.ReleaseComObject(workBook);
        _excelApp.Quit();
于 2012-12-06T14:32:57.407 回答