0 امتیاز

دوستان عزیز

اشکال این کد چی هست که یک رکورد کمتر به فایل اکسل ارسال می کند، جستجو هم انجام دادم ولی به نتیجه نرسیدم، در ضمن من این کد را از آموزش های آقای ابراهیمی که به همین منظور آماده شده بود برداشتم:

                object mis = System.Reflection.Missing.Value;
                Microsoft.Office.Interop.Excel._Application app = new Microsoft.Office.Interop.Excel.Application();
                Microsoft.Office.Interop.Excel._Workbook workbook = app.Workbooks.Add(Type.Missing);
                Microsoft.Office.Interop.Excel._Worksheet worksheet = null;
                app.Visible = false;
                worksheet = (Worksheet)workbook.Sheets["Sheet1"];
                worksheet = (Worksheet)workbook.ActiveSheet;
                worksheet.Name = "خروجی";
                for (int i = 4; i < dataGridStudent.Columns.Count + 1; i++)
                {
                    worksheet.Cells[1, i] = dataGridStudent.Columns[i - 1].HeaderText;
                }
                for (int i = 0; i < dataGridStudent.Rows.Count - 1; i++)
                {
                    for (int j = 3; j < dataGridStudent.Columns.Count; j++)
                    {
                        worksheet.Cells[i + 2, j + 1] = dataGridStudent.Rows[i].Cells[j].Value.ToString();
                    }
                }
                Frm_Loding.CloseLoadingScreen();
                SaveFileDialog sfd = new SaveFileDialog();
                sfd.Filter = "Excel Document(*.xlsx)|*.xlsx";
                sfd.FileName = "Export";
                if (sfd.ShowDialog() == DialogResult.OK)
                {
                    workbook.SaveAs(sfd.FileName, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
                }
                app.Quit();

 

بسته شده

2 پاسخ

+1 امتیاز
 
بهترین پاسخ

دوست عزیز اصلاح شد البته مشکل در هم دیتابیس شما بود هم کد من+نگته مثبت این موضوع اینه که از dll اکسل استفاده نمیشه

http://www.w3-farsi.com/forum/?qa=blob&qa_blobid=10367179210394710350

خیلی ممنون از وقتی که گذاشتید، فقط میشه بفرمائید مشکل در دیتابیسم چی بود، که در آینده به مشکل بر نخورم؟
توسط (252 امتیاز) 4 13 22
شما دیتابیش رو راست چین کرده بودبد این عمل زمانی بدرد میخوره که بخایین مستقیم از اکسس استفاده کنین و توی طراحی دیتابیس نمیدونم چکار کرده بودید من یه پروژه ساختم تست کردم جواب مثبت بود اما توی پروژه شما خروجی همونطور بود اول کدهارو عوض کردم نشد اخر سر دیتابیس رو پاک کردم دوبیاره ساختم بدون دستکاری فقط فید و رو ساختم
توسط (7,164 امتیاز) 4 7 141
+1 امتیاز

سلام

من قبلا از این روش استفاده میکرم بعد که پروژه بزرگتر شد وجود کلاس اکسل در دیتاتیبل تداخل ایجاد کرد نمیدونم خیلی تلاش کردم نشد میبایست کل اسم کلاس رو توی هر سطر مینوشتی

  //try
            //{
            //    // creating Excel Application 

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


            //    // creating new WorkBook within Excel application 

            //    _Workbook workbook = app.Workbooks.Add(Type.Missing);


            //    // creating new Excelsheet in workbook 

            //    _Worksheet worksheet = 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 


            //    worksheet = (Worksheet)workbook.Sheets["Sheet1"];

            //    worksheet = (Worksheet)workbook.ActiveSheet;

            //    // changing the name of active sheet 

            //    worksheet.Name = "Exported from gridview";


            //    // storing header part in Excel 

            //    for (int i = 1; i < dataGridView1.Columns.Count + 1; i++)

            //    {

            //        worksheet.Cells[1, i] = dataGridView1.Columns[i - 1].HeaderText;

            //    }

            //    // storing Each row and column value to excel sheet 

            //    for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)

            //    {

            //        for (int j = 0; j < dataGridView1.Columns.Count; j++)

            //        {

            //            worksheet.Cells[i + 2, j + 1] = dataGridView1.Rows[i].Cells[j].Value.ToString();

            //        }

            //    }


            //    // save the application 

            //    // workbook.SaveAs("g:\\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);

            //    SaveFileDialog sfd = new SaveFileDialog();
            //    sfd.Filter = "Excel Document(*.xlsx)|*.xlsx";
            //    // sfd.FileName = "Export";
            //    if (sfd.ShowDialog() == DialogResult.OK)
            //    {
            //        workbook.SaveAs(sfd.FileName, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
            //    }

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

            //}
            //catch (Exception)
            //{

            //    throw;
            //}
            //finally
            //{
            //    endwait();
            //}

بعد بفکر افتادم روش راحت و بی دردسر بدون کلاس و dll ببین 

بسیار راحت فقط لازمه گزینه allowuseraddRow رو منفی کنی


 

 


            string xls = string.Empty;

           
            foreach (DataGridViewColumn column in dataGridView1.Columns)
            {
                xls += column.HeaderText + ',';
            }

        
            xls += "\r\n";

           
            foreach (DataGridViewRow row in dataGridView1.Rows)
            {
                foreach (DataGridViewCell cell in row.Cells)
                {
                
                    xls += cell.Value.ToString().Replace(",", ";") + ',';
                }

              
                xls += "\r\n";
            }

            
            string folderPath = "g:\\";
            File.WriteAllText(folderPath + "myexel.CSV", xls);
            MessageBox.Show("Export To Exel Success");
        }

 

بیزحمت پروژه بزار اینجا درستش کنم
توسط (7,164 امتیاز) 4 7 141
اینم سورس خدمت شما: فقط اشکالاتش به شرح ذیل هست:

1. فونت های فارسی رو پشتیبانی نمیکنه.

2. خروجی میده ولی همه را در یک ستون میده

http://s9.picofile.com/file/8338449126/WindowsFormsApp12.rar.html
توسط (252 امتیاز) 4 13 22
سوال جدید

2,337 سوال

2,871 پاسخ

3,725 دیدگاه

3,920 کاربر

دسته بندی ها

...