Click here to Skip to main content
15,886,664 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
can u tell me how can i save my data in excel file from dataset,in 3 sheets,if rows are 65536*3 in dataset.One thing that i don't want to define 3 sheet,if 1st sheet saved by 65536 rows,then next 65536 rows should save in 2nd sheet and so on in same excel file.plz help bcoz i m trying since 4 days to make a program by which i can save data,from dataset to excel in multiple sheet dynamically.
Posted

C#
for (int i = 1; i <= ViewData.Rows.Count / 65536 + 1; i++)
            {
                xlworkSheet = (Excel.Worksheet)xlworkBook.Worksheets.get_Item(i);
                range = null;
                range = xlworkSheet.get_Range("A1", misValue);
                range = range.get_Resize(DS.Tables[0].Rows.Count,DS.Tables[0].Columns.Count);

                if (65536 * i < DS.Tables[0].Rows.Count)
                {
                    if (i <= 1)
                    {
                        for (RRow = RRow; RRow <= 65536 * i - 1; ++RRow)
                        {
                            for (int RColm = 0; RColm < ViewData.Cols.Count-1; RColm++)
                            {
                                tempArray[RRow, RColm] = DS.Tables[0].Rows[RRow].ItemArray[RColm].ToString();
                            }
                        }

                        range.set_Value(System.Reflection.Missing.Value, tempArray);
                        Array.Clear(tempArray, 0, tempArray.Length);
                        tempArray = new string[ViewData.Rows.Count, ViewData.Cols.Count];

                    }
                    else
                    {
                        int tempRow = 0;
                        for (RRow = RRow; RRow < ViewData.Rows.Count - 1; ++RRow)
                        {
                            for (int RColm = 0; RColm < ViewData.Cols.Count - 1; RColm++)
                            {
                                tempArray[tempRow, RColm] = DS.Tables[0].Rows[RRow].ItemArray[RColm].ToString();

                            }
                            tempRow++;
                        }
                        range.set_Value(System.Reflection.Missing.Value, tempArray);
                    }
                 }
                else
                {
                    int tempRow = 0;
                    for (RRow = RRow; RRow < ViewData.Rows.Count - 1; ++RRow)
                    {
                        for (int RColm = 0; RColm < ViewData.Cols.Count - 1; RColm++)
                        {
                            tempArray[tempRow, RColm] = DS.Tables[0].Rows[RRow].ItemArray[RColm].ToString();
                        }
                        tempRow++;
                    }
                    range.set_Value(System.Reflection.Missing.Value, tempArray);
                    xlworkBook.SaveAs("Myfile.xls", Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);
                }
             }

              xlApp.UserControl = true;
             Process pro = Process.Start("myfilepath");


//this is the logic,I have created and success to make.We can do some better from user point of view
 
Share this answer
 
v2
Use the XLSX file format which can go up to 1 million rows.

Try this component : http://epplus.codeplex.com/[^]
 
Share this answer
 
Comments
StackQ 7-Sep-12 7:36am    
I don't want to use third partt tool.
StackQ 17-Sep-12 5:38am    
Ok,if we r use .xlsx format,then it will handle 1 million rows,but if rows r greater than 1 million,then should be save in 2nd sheet.U can do it through my above code by changing at some point.
faisal23 6-Nov-12 4:44am    
hi 01010RAJ can u plz solve my problem. My current code working but it will display in same sheet. ONly currect this to show data in 2 sheets.
Thank u
faisal23 6-Nov-12 4:44am    
hi 01010RAJ can u plz solve my problem. My current code working but it will display in same sheet. ONly currect this to show data in 2 sheets.
Thank u

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900