【问题标题】:How to transfer data from a dataset to existing excel form's particular worksheet with specific row and columns in c#如何将数据从数据集传输到现有 excel 表单的特定工作表,其中包含 c# 中的特定行和列
【发布时间】:2014-05-14 08:55:56
【问题描述】:

我正在做一个项目,我必须从 DBASE 文件中获取数据,然后导出数据 excel form

excel form 有 10 多个工作表,其中包含用于添加行的按钮。

我已使用OLEDBdbase 文件中检索到所需格式的数据(如excel form 所要求的)。

现在的问题是如何将数据从数据表发送到具有所需行和列的 Excel 表单特定工作表。

另外,如何向工作表的按钮发送命令以添加所需的行?

这就是我从 dbase 文件中获取数据并将其存储到数据表中的方式。

private void button1_Click(object sender, EventArgs e)
{

   string a = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=d:\\vat;Extended     Properties =dBASE IV; User ID = Admin ;Password =";
   OleDbConnection con = new OleDbConnection();
   con.ConnectionString = a;
   con.Open();
   //MessageBox.Show(con.State.ToString());
   string qu = "Select * from abc.dbf ";
   OleDbDataAdapter oda = new OleDbDataAdapter(qu, con);
   DataTable dt = new DataTable();
   oda.Fill(dt);
   this.dataGridView1.DataSource = dt;

}

有很多示例可以将数据从数据表导出到 Excel,但几乎在每个示例中都会创建一个新的 Excel 表单。我必须导出到包含大约 10 张纸的现有 excel 表单。现在我想将该数据表内容插入名为 ABC.XLS 的 Excel 工作表,工作表名称为 A。

我希望我能解释我的问题。

【问题讨论】:

  • 如果您直接从数据库中将数据添加到 excel 工作表中会怎样?当然,您可以从您的 c# 代码中添加它。
  • 您可以在 sql server 中使用这一行插入到 excel - 插入到 OPENROWSET('Microsoft.Jet.OLEDB.4.0', 'Excel 8.0;Database=D:\testing.xls; ', 'SELECT * FROM [SheetName$]') 从 YourSQLServerTable 中选择 *
  • 您能详细说明一下吗?
  • 在数组的帮助下用c#可以吗?
  • 当然它在 c# 中是可能的。通过创建 - Microsoft.Office.Interop.Excel.Application 的实例,您可以实现它,但首先向我展示您到目前为止所做的事情。以便我能以更好的方式帮助您...

标签: c# excel dataset dbase


【解决方案1】:

以下答案可能无法满足您的确切要求,但只需验证即可。 顺便说一句,很抱歉回复晚了,因为我正忙于我现有的工作。

下面的代码我在我的一个桌面应用程序中使用过。希望对您有所帮助。

public static void ExportToExcel(DataTable dt)
        {
            Microsoft.Office.Interop.Excel.Application excelApp = null;
            try
            {
                // instantiating the excel application class
                excelApp = new Microsoft.Office.Interop.Excel.Application();
                Microsoft.Office.Interop.Excel.Workbook currentWorkbook = excelApp.Workbooks.Add(Type.Missing);
                Microsoft.Office.Interop.Excel.Worksheet currentWorksheet = (Microsoft.Office.Interop.Excel.Worksheet)currentWorkbook.ActiveSheet;
                currentWorksheet.Columns.ColumnWidth = 18;


                if (dt.Rows.Count > 0)
                {
                    currentWorksheet.Cells[1, 1] = DateTime.Now.ToString("s");
                    int i = 1;
                    foreach (DataColumn dtColumn in dt.Columns)
                    {
                        // Excel work sheet indexing starts with 1
                        currentWorksheet.Cells[2, i] = dtColumn.Name;
                        ++i;
                    }
                    Microsoft.Office.Interop.Excel.Range headerColumnRange = currentWorksheet.get_Range("A2", "G2");
                    headerColumnRange.Font.Bold = true;
                    headerColumnRange.Font.Color = 0xFF0000;
                    //headerColumnRange.EntireColumn.AutoFit();
                    int rowIndex = 0;
                    for (rowIndex = 0; rowIndex < dt.Rows.Count; rowIndex++)
                    {
                        DataRow row = dt.Rows[rowIndex];
                        for (int cellIndex = 0; cellIndex < row.Cells.Count; cellIndex++)
                        {
                            currentWorksheet.Cells[rowIndex + 3, cellIndex + 1] = row.Cells[cellIndex].Value;
                        }
                    }
                    Microsoft.Office.Interop.Excel.Range fullTextRange = currentWorksheet.get_Range("A1", "G" + (rowIndex + 1).ToString());
                    fullTextRange.WrapText = true;
                    fullTextRange.HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignLeft;
                }
                else
                {
                    string timeStamp = DateTime.Now.ToString("s");
                    timeStamp = timeStamp.Replace(':', '-');
                    timeStamp = timeStamp.Replace("T", "__");
                    currentWorksheet.Cells[1, 1] = timeStamp;
                    currentWorksheet.Cells[1, 2] = "No error occured";
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Exception", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            {
                if (excelApp != null)
                {
                    excelApp.Quit();
                }
            }
        }

仅供参考MSDN Link

祝你好运……

【讨论】:

  • 我无法从 Excel 文件中打开特定的工作表,我正在为这本书使用以下代码。Open(mysheet, 0, true, 5, "", "", true, Microsoft.Office .Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0); Excel.Worksheet xlWorkSheetFocus = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(3); xlWorkSheetFocus.Activate();
  • 但是它会打开所需的表单,但默认情况下不会将工作表 3 设置为当前工作表。我收到错误对象引用未设置为对象的实例。我如何解决这个@Krishnraj Rana
  • 打开所需的工作表后,我可以尝试您的代码
  • 在我的回答中,我假设您的 DataTable dt 中有数据。那么你到底在哪里出错???
  • 是的,我在数据表中有数据。但我想将数据传输到特定的 Excel 文件表,根据我的做法,我需要首先打开 excel 表单并将所需的表设置为活动表。上面提到的代码(在 cmets 中)对我不起作用。我只是卡住了。我希望我已经表达了我的问题。请帮助@Krishnraj Rana 先生
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-03-26
  • 1970-01-01
  • 1970-01-01
  • 2015-04-03
  • 2012-07-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多