Register Register Member Login Member Login Member Login Forgot Password ??
PHP , ASP , ASP.NET, VB.NET, C#, Java , jQuery , Android , iOS , Windows Phone
 

Registered : 109,028

HOME > .NET Framework > Forum > ดาต้าเยอะมาก ประมาณหลักแสน จะexport ใส่ excel ไม่ได้ เพราะเยอะเกิน 1 Sheet มันสามารถเเบ่งข้อมูล เป็น Sheet1 ,Sheet2 ได้ไหมคะ



 

ดาต้าเยอะมาก ประมาณหลักแสน จะexport ใส่ excel ไม่ได้ เพราะเยอะเกิน 1 Sheet มันสามารถเเบ่งข้อมูล เป็น Sheet1 ,Sheet2 ได้ไหมคะ

 



Topic : 083580



โพสกระทู้ ( 61 )
บทความ ( 0 )



สถานะออฟไลน์
Facebook



ดาต้าเยอะมาก ประมาณหลักแสน จะ export ใส่ excel ไม่ได้ เพราะเยอะเกิน 1 Sheet มันสามารถเเบ่งข้อมูล เป็น Sheet1 ,Sheet2 ได้ไหมคะ
(C#.net)

ขอบพระคุณมากคะ

ถ้าอธิบายไม่เข้าใจบอกด้วยนะคะ



Tag : .NET, Ms SQL Server 2008, Web (ASP.NET), C#









ประวัติการแก้ไข
2012-09-06 22:18:01
2012-09-07 13:37:49
Move To Hilight (Stock) 
Send To Friend.Bookmark.
Date : 2012-09-06 22:17:15 By : nongnoo View : 1668 Reply : 8
 

 

No. 1



โพสกระทู้ ( 74,058 )
บทความ ( 838 )

สมาชิกที่ใส่เสื้อไทยครีเอท

สถานะออฟไลน์
Twitter Facebook

ได้ครับ คุณจะต้อง Loop นับเองครับ ถ้าเกินหลักแสนก็ให้ Create Sheet ใหม่ครับ ทำได้ไม่ยากครับ






แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2012-09-08 06:58:46 By : mr.win
 


 

No. 2



โพสกระทู้ ( 61 )
บทความ ( 0 )



สถานะออฟไลน์
Facebook

ขอโทษคะ เเล้วจะ Create Sheet เขียนยังไงคะ พอมีตัวอย่างไหมคะ
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2012-09-09 13:57:49 By : nongnoo
 

 

No. 3



โพสกระทู้ ( 169 )
บทความ ( 0 )



สถานะออฟไลน์
Facebook Blogger

ลองดูอันนี้นะครับก็อบคนอื่นมา

Code (C#)
Microsoft.Office.Interop.Excel.Application xlApp = null;
        Excel.Workbook xlWorkbook = null;
        Excel.Sheets xlSheets = null;
        Excel.Worksheet xlNewSheet = null;
        string worksheetName ="Sheet_Name";
        object readOnly1 = false;

        object isVisible = true;

        object missing = System.Reflection.Missing.Value;

            try
            {
                xlApp = new Microsoft.Office.Interop.Excel.Application();

                if (xlApp == null)
                    return;

                // Uncomment the line below if you want to see what's happening in Excel
                // xlApp.Visible = true;

                xlWorkbook = xlApp.Workbooks.Open(@"C:\Book1.xls", missing, readOnly1, missing, missing, missing, missing, missing, missing, missing, missing, isVisible, missing, missing, missing);

                xlSheets = (Excel.Sheets)xlWorkbook.Sheets;

                // The first argument below inserts the new worksheet as the first one
                xlNewSheet = (Excel.Worksheet)xlSheets.Add(xlSheets[1], Type.Missing, Type.Missing, Type.Missing);
                xlNewSheet.Name = worksheetName;

                xlWorkbook.Save();
                xlWorkbook.Close(Type.Missing, Type.Missing, Type.Missing);
                xlApp.Quit();
            }
            finally
            {
                System.Runtime.InteropServices.Marshal.ReleaseComObject(xlNewSheet);
                System.Runtime.InteropServices.Marshal.ReleaseComObject(xlSheets);
                System.Runtime.InteropServices.Marshal.ReleaseComObject(xlWorkbook);
                System.Runtime.InteropServices.Marshal.ReleaseComObject(xlApp);
                //xlApp = null;
            }

แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2012-09-09 14:41:43 By : pStudio
 


 

No. 4



โพสกระทู้ ( 61 )
บทความ ( 0 )



สถานะออฟไลน์
Facebook

ไม่ได้อะคะ T T
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2012-09-09 23:00:51 By : nongnoo
 


 

No. 5

Guest


มีข้อมูลหลักแสน ให้ export เป็น xlsx ครับ

เพราะรองรับได้เป็นล้าน (ผมใช้ EPPlus https://www.thaicreate.com/dotnet/forum/080303.html)

ส่วนจะ add sheet ของ xls ด้วย Microsoft.Office.Interop.Excel ทำแบบนี้ครับ

Code (C#)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Excel = Microsoft.Office.Interop.Excel;
using System.Data;
using System.IO;

namespace xxx
{
    class ExportExcel : IDisposable
    {
        private object missing = Type.Missing;
        private object readOnly = true;

        private Excel.Application xlApp;
        private Excel.Workbook xlBook;
        private Excel.Range xlRange;

        private string filePath;
        private string message;
        private int sheetCount;
        private int rowCount;

        public ExportExcel(string FilePath)
        {
            filePath = FilePath;
            message = "Hello World!!!";
            sheetCount = 0;
            rowCount = 0;

            xlApp = new Excel.Application();
            xlApp.Visible = false;
            xlApp.ScreenUpdating = false;
            xlApp.DisplayAlerts = false;
        }

        public bool Export(DataSet DsData)
        {
            if(!Create())
                return false;

            foreach (DataTable DtData in DsData.Tables)
            {
                Excel.Worksheet xlSheet = (Excel.Worksheet)xlBook.Worksheets.Add(missing, missing, missing, missing);

                if(!FillData(xlSheet, DtData))
                    return false;

                sheetCount++;
            }

            if(!Save())
                return false;

            message = string.Format("Export '{0}' Success. ({1} Sheets, {2} Rows)", Path.GetFileName(filePath), sheetCount.ToString("#,##0"), rowCount.ToString("#,##0"));

            return true;
        }

        public bool Export(DataTable DtData)
        {
            if (!Create())
                return false;

            Excel.Worksheet xlSheet = (Excel.Worksheet)xlBook.Worksheets.Add(missing, missing, missing, missing);

            if (!FillData(xlSheet, DtData))
                return false;

            sheetCount++;

            if (!Save())
                return false;

            message = string.Format("Export '{0}' Success. ({1} Sheets, {2} Rows)", Path.GetFileName(filePath), sheetCount.ToString("#,##0"), rowCount.ToString("#,##0"));

            return true;
        }

        public bool Export(DataSet DsData, string TableName)
        {
            return Export(DsData.Tables[TableName]);
        }

        public bool Export(DataSet DsData, int TableIndex)
        {
            return Export(DsData.Tables[TableIndex]);
        }

        protected bool Create()
        {
            bool result = false;

            if (!File.Exists(filePath))
                File.Delete(filePath);

            try
            {
                xlBook = xlApp.Workbooks.Add(Microsoft.Office.Interop.Excel.XlWBATemplate.xlWBATWorksheet);
                result = true;
            }
            catch (System.Runtime.InteropServices.COMException ex)
            {
                message = ex.Message;
                Dispose();
            }

            return result;
        }

        protected bool Save()
        {
            bool result = false;

            try
            {
                xlBook.SaveAs(filePath, missing, missing, missing, missing, missing, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange, missing, missing, missing, missing, missing);
                result = true;
            }
            catch (System.Runtime.InteropServices.COMException ex)
            {
                message = ex.Message;
                Dispose();          
            }

            return result;       
        }

        protected bool FillData(Excel.Worksheet xlSheet, DataTable DtData)
        {
            int row = 2;
            int col = 1;

            xlSheet.Name = DtData.TableName;

            foreach (DataColumn Dc in DtData.Columns)
            {
                xlSheet.Cells[1, col] = string.Format("'{0}", Dc.ColumnName);
                xlRange = (Excel.Range)xlSheet.Cells[1, col];
                xlRange.Font.Name = "MS Sans Serif";
                xlRange.Font.Size = 10;
                xlRange.Interior.Color = System.Drawing.Color.LightYellow;
                xlRange.Borders.Color = System.Drawing.Color.LightGray;

                col++;
            }

            foreach (DataRow Dr in DtData.Rows)
            {
                col = 1;

                foreach (DataColumn Dc in DtData.Columns)
                {
                    xlRange = (Excel.Range)xlSheet.Cells[row, col];
                    xlRange.Font.Name = "MS Sans Serif";
                    xlRange.Font.Size = 10;
                    xlRange.Borders.Color = System.Drawing.Color.LightGray;

                    if (Dc.DataType == typeof(double) || Dc.DataType == typeof(float))
                    {
                        try
                        {
                            xlSheet.Cells[row, col] = Dr[col - 1];
                            xlRange.NumberFormat = "#,##0.00";
                        }
                        catch (System.Runtime.InteropServices.COMException ex)
                        {
                            message = ex.Message;
                            Dispose();

                            return false;
                        }
                    }
                    else if (Dc.DataType == typeof(int) || Dc.DataType == typeof(short) || Dc.DataType == typeof(ushort) ||
                        Dc.DataType == typeof(uint) || Dc.DataType == typeof(long) || Dc.DataType == typeof(ulong) ||
                        Dc.DataType == typeof(decimal))
                    {
                        try
                        {
                            xlSheet.Cells[row, col] = Dr[col - 1];
                            xlRange.NumberFormat = "#,##0";
                        }
                        catch (System.Runtime.InteropServices.COMException ex)
                        {
                            message = ex.Message;
                            Dispose();

                            return false;
                        }
                    }
                    else if (Dc.DataType == typeof(DateTime))
                    {
                        try
                        {
                            xlSheet.Cells[row, col] = Dr[col - 1];
                        }
                        catch (System.Runtime.InteropServices.COMException ex)
                        {
                            message = ex.Message;
                            Dispose();

                            return false;
                        }
                    }
                    else if (Dc.DataType == typeof(bool))
                    {
                        try
                        {
                            xlSheet.Cells[row, col] = Dr[col - 1];
                        }
                        catch (System.Runtime.InteropServices.COMException ex)
                        {
                            message = ex.Message;
                            Dispose();

                            return false;
                        }
                    }
                    else if (Dc.DataType == typeof(sbyte) || Dc.DataType == typeof(byte) || Dc.DataType == typeof(byte[]))
                    {
                        try
                        {
                            xlSheet.Cells[row, col] = null;
                        }
                        catch (System.Runtime.InteropServices.COMException ex)
                        {
                            message = ex.Message;
                            Dispose();

                            return false;
                        }
                    }
                    else
                    {
                        try
                        {
                            xlSheet.Cells[row, col] = (!string.IsNullOrEmpty(Dr[col - 1].ToString())) ? string.Format("'{0}", Dr[col - 1]) : null;
                        }
                        catch (System.Runtime.InteropServices.COMException ex)
                        {
                            message = ex.Message;
                            Dispose();

                            return false;
                        }
                    }

                    col++;
                }

                rowCount++;
                row++;
            }

            return true;
        }

        public string Message
        {
            get { return message; }
        }

        public void Dispose()
        {
            if (xlApp != null)
            {
                xlApp.Quit();

                System.Runtime.InteropServices.Marshal.ReleaseComObject(xlRange);
                System.Runtime.InteropServices.Marshal.ReleaseComObject(xlBook);
                System.Runtime.InteropServices.Marshal.ReleaseComObject(xlApp);

                GC.SuppressFinalize(xlRange);
                GC.SuppressFinalize(xlBook);
                GC.SuppressFinalize(xlApp);
                GC.Collect();

                xlRange = null;
                xlBook = null;
                xlApp = null;
            }
        }
    }
}



เวลาเรียกใช้

Code (C#)
        private void buttonExport_Click(object sender, EventArgs e)
        {
            saveFileDialog1 = new SaveFileDialog();
            saveFileDialog1.Filter = "Excel Files|*.xlsx|Excel 97-2003 Files|*.xls|All Files|*.*";
            saveFileDialog1.Title = "Export to Excel File";

            DialogResult result = saveFileDialog1.ShowDialog();

            if (result == DialogResult.OK && saveFileDialog1.FileName != string.Empty)
            {
                backgroundWorker1.RunWorkerAsync(saveFileDialog1.FileName);
            }
        }

        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            using (ExportExcel excel = new ExportExcel((string)e.Argument))
            {
                DataSet DsData = GetExportData();

                bool result = excel.Export(DsData);
                excel.Dispose();

                if (!result)
                {
                    MessageBox.Show(excel.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                else
                {
                    MessageBox.Show(excel.Message, "Success", MessageBoxButtons.OK);
                }
            }
        }

แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2012-09-10 08:13:21 By : ห้ามตอบเกินวันละ 2 กระทู้
 


 

No. 6



โพสกระทู้ ( 61 )
บทความ ( 0 )



สถานะออฟไลน์
Facebook

ขอบคุณมากคะ
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2012-09-15 23:28:52 By : nongnoo
 


 

No. 7



โพสกระทู้ ( 61 )
บทความ ( 0 )



สถานะออฟไลน์
Facebook

ขอโทษนะคะ เอาไปใช้เเล้ว erorr ช่วยบอกทีคะ
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2012-11-19 15:51:33 By : nongnoo
 


 

No. 8

Guest


ERROR ยังไงอะคะ มันมีเป็นล้านแปดERRORอะค่ะ ใครจะช่วยได้คะตอบมาลอยๆ.
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2012-11-26 18:09:50 By : me
 

   

ค้นหาข้อมูล


   
 

แสดงความคิดเห็น
Re : ดาต้าเยอะมาก ประมาณหลักแสน จะexport ใส่ excel ไม่ได้ เพราะเยอะเกิน 1 Sheet มันสามารถเเบ่งข้อมูล เป็น Sheet1 ,Sheet2 ได้ไหมคะ
 
 
รายละเอียด
 
ตัวหนา ตัวเอียง ตัวขีดเส้นใต้ ตัวมีขีดกลาง| ตัวเรืองแสง ตัวมีเงา ตัวอักษรวิ่ง| จัดย่อหน้าอิสระ จัดย่อหน้าชิดซ้าย จัดย่อหน้ากึ่งกลาง จัดย่อหน้าชิดขวา| เส้นขวาง| ขนาดตัวอักษร แบบตัวอักษร
ใส่แฟลช ใส่รูป ใส่ไฮเปอร์ลิ้งค์ ใส่อีเมล์ ใส่ลิ้งค์ FTP| ใส่แถวของตาราง ใส่คอลัมน์ตาราง| ตัวยก ตัวห้อย ตัวพิมพ์ดีด| ใส่โค้ด ใส่การอ้างถึงคำพูด| ใส่ลีสต์
smiley for :lol: smiley for :ken: smiley for :D smiley for :) smiley for ;) smiley for :eek: smiley for :geek: smiley for :roll: smiley for :erm: smiley for :cool: smiley for :blank: smiley for :idea: smiley for :ehh: smiley for :aargh: smiley for :evil:
Insert PHP Code
Insert ASP Code
Insert VB.NET Code Insert C#.NET Code Insert JavaScript Code Insert C#.NET Code
Insert Java Code
Insert Android Code
Insert Objective-C Code
Insert XML Code
Insert SQL Code
Insert Code
เพื่อความเรียบร้อยของข้อความ ควรจัดรูปแบบให้พอดีกับขนาดของหน้าจอ เพื่อง่ายต่อการอ่านและสบายตา และตรวจสอบภาษาไทยให้ถูกต้อง

อัพโหลดแทรกรูปภาพ

Notice

เพื่อความปลอดภัยของเว็บบอร์ด ไม่อนุญาติให้แทรก แท็ก [img]....[/img] โดยการอัพโหลดไฟล์รูปจากที่อื่น เช่นเว็บไซต์ ฟรีอัพโหลดต่าง ๆ
อัพโหลดแทรกรูปภาพ ให้ใช้บริการอัพโหลดไฟล์ของไทยครีเอท และตัดรูปภาพให้พอดีกับสกรีน เพื่อความโหลดเร็วและไฟล์ไม่ถูกลบทิ้ง

   
  เพื่อความปลอดภัยและการตรวจสอบ กระทู้ที่แทรกไฟล์อัพโหลดไฟล์จากที่อื่น อาจจะถูกลบทิ้ง
 
โดย
อีเมล์
บวกค่าให้ถูก
<= ตัวเลขฮินดูอารบิก เช่น 123 (หรือล็อกอินเข้าระบบสมาชิกเพื่อไม่ต้องกรอก)







Exchange: นำเข้าสินค้าจากจีน, Taobao, เฟอร์นิเจอร์, ของพรีเมี่ยม, ร่ม, ปากกา, power bank, แฟลชไดร์ฟ, กระบอกน้ำ

Load balance : Server 04
ThaiCreate.Com Logo
© www.ThaiCreate.Com. 2003-2024 All Rights Reserved.
ไทยครีเอทบริการ จัดทำดูแลแก้ไข Web Application ทุกรูปแบบ (PHP, .Net Application, VB.Net, C#)
[Conditions Privacy Statement] ติดต่อโฆษณา 081-987-6107 อัตราราคา คลิกที่นี่