0

我目前在 Excel 电子表格中打开和阅读以下代码:

var connectionString = string.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=Excel 12.0;", fileNameTextBox.Text);
var queryString = String.Format("SELECT * FROM [{0}]",DETAILS_SHEET_NAME);
var adapter = new OleDbDataAdapter(queryString, connectionString);
var ds = new DataSet();

adapter.Fill(ds, DETAILS_SHEET_NAME);

DataTable data = ds.Tables[DETAILS_SHEET_NAME];

dataGridView1.DataSource = data;
                dataGridView1.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCellsExceptHeader);

这一切都很好,除了我对工作表的第一行(可能是前两行,因为第 2 行是标题)不感兴趣。如何修改选择查询以选择一个范围,就像我在 excel 中一样?

我有兴趣在从 2 开始的所有行中读取包含数据的行中的列 AN。

我还需要访问不同工作表上的几个特定单元格,我假设我必须为每个单元格构建另一个具有不同查询字符串的适配器?

4

2 回答 2

2

修改Select语句只包括您需要的列而不是通配符"*",如下例所示:

("SELECT Column1, Column2 FROM DETAILS_SHEET_NAME");

您可以应用其他逻辑来删除不必要的行,例如,“分页解决方案”(即从 N 到 M 选择行),如下所示:

假设数据库表“TBL_ITEM”包含两个感兴趣的列(字段):“Item”列,代表唯一ID和“Rank”,用于升序排序,一般分页问题如下: Select N - 表中按 Rank 偏移(即跳过)(MN)行排序的行:

SELECT TOP N Item, 
Rank FROM (SELECT TOP M Rank, Item FROM TBL_ITEM ORDER BY Rank) 
AS [SUB_TAB] ORDER BY Rank DESC 

这个解决方案及其扩展/示例在我的文章Pure SQL solution to Database Table Paging(链接: http: //www.codeproject.com/Tips/441079/Pure-SQL-solution-to-Database-Table-Paging )

最后,您可以使用清单 2 中所示的代码片段将 DataTable 对象的内容导出到 Excel 文件中,其中包含大量可以添加到代码中的自定义功能;

清单 2. 将 DataTable 导出到 Excel 文件 (2007/2010):

internal static bool Export2Excel(DataTable dataTable, bool Interactive) 
{
    object misValue = System.Reflection.Missing.Value;

    // Note: don't include Microsoft.Office.Interop.Excel in reference (using),
    // it will cause ambiguity w/System.Data: both have DataTable obj
    Microsoft.Office.Interop.Excel.Application _appExcel = null;
    Microsoft.Office.Interop.Excel.Workbook _excelWorkbook = null;
    Microsoft.Office.Interop.Excel.Worksheet _excelWorksheet = null;
    try
    {
        // excel app object
        _appExcel = new Microsoft.Office.Interop.Excel.Application();

        // make it visible to User if Interactive flag is set
        _appExcel.Visible = Interactive;

        // excel workbook object added to app
        _excelWorkbook = _appExcel.Workbooks.Add(misValue);

        _excelWorksheet = _appExcel.ActiveWorkbook.ActiveSheet 
            as Microsoft.Office.Interop.Excel.Worksheet;

        // column names row (range obj)
        Microsoft.Office.Interop.Excel.Range _columnsNameRange;
        _columnsNameRange = _excelWorksheet.get_Range("A1", misValue);
        _columnsNameRange = _columnsNameRange.get_Resize(1, dataTable.Columns.Count);

        // data range obj
        Microsoft.Office.Interop.Excel.Range _dataRange;
        _dataRange = _excelWorksheet.get_Range("A2", misValue);
        _dataRange = _dataRange.get_Resize(dataTable.Rows.Count, dataTable.Columns.Count);

        // column names array to be assigned to columnNameRange
        string[] _arrColumnNames = new string[dataTable.Columns.Count];

        // 2d-array of data to be assigned to _dataRange
        string[,] _arrData = new string[dataTable.Rows.Count, dataTable.Columns.Count];

        // populate both arrays: _arrColumnNames and _arrData
        // note: 2d-array structured as row[idx=0], col[idx=1]
        for (int i = 0; i < dataTable.Columns.Count; i++)
        {
            for (int j = 0; j < dataTable.Rows.Count; j++)
            {
                _arrColumnNames[i] = dataTable.Columns[i].ColumnName;
                _arrData[j, i] = dataTable.Rows[j][i].ToString();
            }
        }

        //assign column names array to _columnsNameRange obj
        _columnsNameRange.set_Value(misValue, _arrColumnNames);

        //assign data array to _dataRange obj
        _dataRange.set_Value(misValue, _arrData);

        // save and close if Interactive flag not set
        if (!Interactive)
        {
            // Excel 2010 - "14.0"
            // Excel 2007 - "12.0"
            string _ver = _appExcel.Version;

            string _fileName ="TableExport_" + 
                DateTime.Today.ToString("yyyy_MM_dd") + "-" +
                DateTime.Now.ToString("hh_mm_ss");

            // check version and select file extension
            if (_ver == "14.0" || _ver == "12.0")  { _fileName += ".xlsx";}
            else { _fileName += ".xls"; }

            // save and close Excel workbook
            _excelWorkbook.Close(true, "{DRIVE LETTER}:\\" + _fileName, misValue);
        }
        return true;
    }
    catch (Exception ex) {  throw; }
    finally
    {
        // quit excel app process
        if (_appExcel != null)
        {
            _appExcel.UserControl = false;
            _appExcel.Quit();
            _appExcel = null;
            misValue = null;
        }
    }
}
于 2013-05-10T16:34:39.967 回答
0

你可以简单地要求没有标题。修改你的连接字符串,添加HDR=No.

对于你的第二个问题,我找到了这篇文章,也许你会发现它有帮助。

于 2013-05-10T16:36:59.223 回答