Tuesday, 14 October 2014

Convert Dataset To EXCEL or NOTEPAD

 Convert Dataset to EXCEL


using System.Xml.Schema;
using System.Xml;
using System.IO;
using Excel = Microsoft.Office.Interop.Excel;

private void button_Clicked(object sender, EventArgs e)
        {
            ExportToExcel();
        }
        public DataTable ExportToExcel()
        {
            string sql = 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.ApplicationClass();
            xlWorkBook = xlApp.Workbooks.Add(misValue);
            xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
          
                             DataSet ds = new DataSet();


             //fill your dataset with any data



                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;
                    }
                }

                xlWorkBook.SaveAs(@"D:\ESIReports.xls", Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);
              
               
                 xlWorkBook.Close(true, misValue, misValue);
                xlApp.Quit();

                //NOTE PAD file or .CSV FILE CREATION
                CSVNotepad(any);
                MessageBox.Show("Excel file created , you can find the file ESIReport.xls");
                return table;

            }
        }










      



 Convert Dataset to NOTEPAD


 private void button_Clicked(object sender, EventArgs e)
        {
            CSVNotepad(DataTableObj)
        }

 public  void CSVNotepad(DataTable dsa)
        {
            DataTable sourceTable = new DataTable();
            sourceTable = dsa;
    
            using (StreamWriter writer = new StreamWriter(@"D:\ESIReports.txt"))//.txt or .csv
            {
                Rfc4180Writer.WriteDataTable(sourceTable, writer, true);
            }
        }



//REQUIRED CLASS FILE FOR NOTEPAD EXECUTION
    public static class Rfc4180Writer
    {
        public static void WriteDataTable(DataTable sourceTable, TextWriter writer, bool includeHeaders)
        {
            //if (includeHeaders)
            //{
            //    List<string> headerValues = new List<string>();
            //    foreach (DataColumn column in sourceTable.Columns)
            //    {
            //        headerValues.Add(QuoteValue(column.ColumnName));
            //    }

            //    writer.WriteLine(String.Join(",", headerValues.ToArray()));
            //}

            string[] items = null;
            foreach (DataRow row in sourceTable.Rows)
            {
                items = row.ItemArray.Select(o => QuoteValue(o.ToString())).ToArray();
                writer.WriteLine(String.Join(",", items));
            }

            writer.Flush();
        }

        private static string QuoteValue(string value)
        {
            return String.Concat("\"", value.Replace("\"", "\"\""), "\"");
        }
    }

No comments:

Post a Comment

Print Only Grid View in ASP.net

ASP.net How to Print Only GridView < div id ="gridviewDiv" >   < asp:GridView ID ="gridViewToPri...