使用WPF将datamysql导出到Excel工作表中的特定单元格中 [英] Export datamysql into specific cells in excel sheet using WPF

查看:81
本文介绍了使用WPF将datamysql导出到Excel工作表中的特定单元格中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用wpf将MySQL dataTable导出到Excel工作表,这可以完成。

我的导出数据将从Excel工作表的单元格A1开始粘贴。



是否有可能将其放入特定的单元格?我需要将数据框导出到从单元格A5开始的范围。这可能吗?



我尝试过:



这里是我的导出代码完美运行:



I need to export MySQL dataTable using wpf to an Excel sheet, which can be done.
My exported data will then be pasted beginning in cell "A1" of the Excel sheet.

Is there a possibility to put it into a specific cell? I need to export the data frame to a range beginning at cell A5. Is this possible?

What I have tried:

here is my exporting code it works perfectly :

private void export_Click(object sender, RoutedEventArgs e)
        {
            using (var conx = new MySqlConnection(constring))
            {
                conn.Open();

            //// extraction excel 

            string Mysql = null;
            string data = null;
            int i = 0;
            int j = 0;

            Excel.Application xlApp;
            Excel.Workbook xlWorkBook;
            Excel.Worksheet xlWorkSheet;

            object misValue = System.Reflection.Missing.Value;

            xlApp = new Excel.Application();
            xlApp.Visible = true;
            xlWorkBook = xlApp.Workbooks.Add(misValue);
            xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);


            Mysql = "SELECT * from database.Table";

            MySqlDataAdapter cmd = new MySqlDataAdapter(Mysql,conn);
            DataSet ds = new DataSet();
            cmd.Fill(ds);

            for (i = 0; i <= ds.Tables[0].Rows.Count-1 ; i++)
            {
                for (j = 0; j <= ds.Tables[0].Columns.Count - 1; j++)
                {
                    data = ds.Tables[0].Rows[i].ItemArray[j].ToString();
                    
                    xlWorkSheet.Cells[ i+1, j+1] = data;


                }

            }
           }

           }

推荐答案

通常,您不使用此代码,

Usually, you don't use this code,
xlWorkSheet.Cells[ i+1, j+1] = data;



它被替换为类似


and it is replaced by something like

xlWorkSheet.Cells[ i+1, j+1].Value = data;






or

xlWorkSheet.Cells[ i+1, j+1].Formula = data;



取决于数据是什么。


depending in what is the data.

引用:

是否有可能把它放入特定的细胞?我需要将数据框导出到从单元格A5开始的范围。这可能吗?

Is there a possibility to put it into a specific cell? I need to export the data frame to a range beginning at cell A5. Is this possible?



您的代码开始粘贴在A1中,只需添加4即可完成。


Your code begin to paste in A1, just add 4 and you are done.

xlWorkSheet.Cells[ i+5, j+1] = data;


这篇关于使用WPF将datamysql导出到Excel工作表中的特定单元格中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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