50٪ تخفیف روی تمام دوره‌ها!
پایان تخفیف تا:
مشاهده دوره‌ها
0

ارسال اطلاعات دیتاگریدویو به اکسل

باسلام خدمت دوستان عزیز.

ما برای انتقال اطلاعات دیتاگریدویو به اکسل از این کد استفاده کردیم:

Microsoft.Office.Interop.Excel._Application app = new Microsoft.Office.Interop.Excel.Application();


            // creating new WorkBook within Excel application
            Microsoft.Office.Interop.Excel._Workbook workbook = app.Workbooks.Add(Type.Missing);


            // creating new Excelsheet in workbook
            Microsoft.Office.Interop.Excel._Worksheet worksheet1 = null;
            Microsoft.Office.Interop.Excel._Worksheet worksheet2 = null;
            Microsoft.Office.Interop.Excel._Worksheet worksheet3 = null;
            Microsoft.Office.Interop.Excel._Worksheet worksheet4 = null;
            Microsoft.Office.Interop.Excel._Worksheet worksheet5 = null;
            // see the excel sheet behind the program
            app.Visible = true;

            // get the reference of first sheet. By default its name is Sheet1.
            // store its reference to worksheet
            worksheet1 = workbook.Sheets["sheet1"];
           worksheet1 = workbook.ActiveSheet;
            worksheet1.Name ="فروش بالاتر از حد" ;

            worksheet2 = workbook.Sheets["sheet2"];
           worksheet2 = workbook.ActiveSheet;
            worksheet2.Name = "فروش پایین تر از حد";

            worksheet3 = workbook.Sheets["sheet3"];
            worksheet3 = workbook.ActiveSheet;
            worksheet3.Name ="خرید بالاتر از حد" ;
           _* //worksheet4 = workbook.Sheets["sheet4"];
            worksheet4 = workbook.ActiveSheet;
            //worksheet4.Name = "خرید پایین تر از حد";
            //worksheet5 = workbook.Sheets["sheet5"];
            worksheet5 = workbook.ActiveSheet;
            //worksheet5.Name = "امتناع";_*
             //changing the name of active sheet
            try
            {
                //dgsaleup
                // storing header part in Excel
                for (int i = 1; i < dgsaleup.Columns.Count + 1; i++)
                {
                    worksheet1.Cells[1, i] = dgsaleup.Columns[i - 1].HeaderText;
                }
                // storing Each row and column value to excel sheet
                for (int i = 0; i < dgsaleup.Rows.Count-1; i++)
                {
                    for (int j = 0; j < dgsaleup.Columns.Count; j++)
                    {
                        worksheet1.Cells[i + 2, j + 1] = dgsaleup.Rows[i].Cells[j].Value.ToString();
                    }
                }
                //dgsaledown
                // storing header part in Excel
                for (int a = 1; a < dgsaledown.Columns.Count + 1; a++)
                {
                    worksheet2.Cells[1, a] = dgsaledown.Columns[a - 1].HeaderText;
                }
                // storing Each row and column value to excel sheet
                for (int a = 0; a < dgsaledown.Rows.Count-1; a++)
                {
                    for (int b = 0; b < dgsaledown.Columns.Count; b++)
                    {
                        worksheet2.Cells[a + 2, b + 1] = dgsaledown.Rows[a].Cells[b].Value.ToString();
                    }
                }
                //dgbuyup
                // storing header part in Excel
                for (int i = 1; i < dgbuyup.Columns.Count + 1; i++)
                {
                    worksheet3.Cells[1, i] = dgbuyup.Columns[i - 1].HeaderText;
                }
                // storing Each row and column value to excel sheet
                for (int i = 0; i < dgbuyup.Rows.Count-1; i++)
                {
                    for (int j = 0; j < dgbuyup.Columns.Count; j++)
                    {
                        worksheet3.Cells[i + 2, j + 1] = dgbuyup.Rows[i].Cells[j].Value.ToString();
                    }
                }
                ////dgbuydown
                //// storing header part in Excel
                //for (int i = 1; i < dgbuydown.Columns.Count + 1; i++)
                //{
                //    worksheet4.Cells[1, i] = dgbuydown.Columns[i - 1].HeaderText;
                //}
                //// storing Each row and column value to excel sheet
                //for (int i = 0; i < dgbuydown.Rows.Count; i++)
                //{
                //    for (int j = 0; j < dgbuydown.Columns.Count; j++)
                //    {
                //        worksheet4.Cells[i + 2, j + 1] = dgbuydown.Rows[i].Cells[j].Value.ToString();
                //    }
                //}
                ////dataGridView5
                //// storing header part in Excel
                //for (int i = 1; i < dataGridView5.Columns.Count + 1; i++)
                //{
                //    worksheet5.Cells[1, i] = dataGridView5.Columns[i - 1].HeaderText;
                //}
                //// storing Each row and column value to excel sheet
                //for (int i = 0; i < dataGridView5.Rows.Count; i++)
                //{
                //    for (int j = 0; j < dataGridView5.Columns.Count; j++)
                //    {
                //        worksheet5.Cells[i + 2, j + 1] = dataGridView5.Rows[i].Cells[j].Value.ToString();
                //    }
                //}
                // save the application
                workbook.SaveAs("I:\\output.xls", Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive, Type.Missing, Type.Missing, Type.Missing, Type.Missing);

                // Exit from the application
                app.Quit();
            }

مشکل اینجاست که موقع ایجاد worksheet4 و worksheet5 خطا داره ولی 3 تای قبلی رو ایجاد میکنه. موقع پر کردن شیت ها هم عملیات رو انجام میده ولی در آخر که داده ها رو نشون میده، فقط تو یک شیت، اونم شیت 3 که آخرین حلقه هست رو نشون میده.

لطفا راهنمایی بفرمایید

پرسیده شده در 1396/12/20 توسط

0 پاسخ

این سوال هنوز پاسخی ندارد

پاسخ شما