?
Solved

How to export a dataview to Excel?

Posted on 2007-10-01
8
Medium Priority
?
853 Views
Last Modified: 2008-02-07
How to export a dataview to excel? It requires the Tab Name and header for each column.
0
Comment
Question by:chuang4630
7 Comments
 
LVL 14

Expert Comment

by:dfu23
ID: 19995740
Hello chuang4630,

The simplest way would be to iterate through the data and output it in Comma Separated Value (CSV) format.

Regards,
dfu23
0
 
LVL 14

Expert Comment

by:Daniel Junges
ID: 20000395
0
 
LVL 14

Accepted Solution

by:
Daniel Junges earned 672 total points
ID: 20000419
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 7

Assisted Solution

by:bungHoc
bungHoc earned 664 total points
ID: 20005399
Yup... I also agree with export to CSV format coz I've had problems with different Office version.

Here is to CSV:

private string ToCSV(DataTable dataTable)
{
      StringBuilder sb = new StringBuilder();      
      foreach (DataColumn column in dataTable.Columns)
      {
            sb.Append(column.ColumnName + ',');
      }            
      sb.Append("\r\n");
      foreach (DataRow row in dataTable.Rows)
      {
            foreach (DataColumn column in dataTable.Columns)
            {
                  sb.Append(row[column].ToString() + ',');
            }                  
            sb.Append("\r\n");
      }      
      return sb.ToString();
}

But if you need to Excel:

public void ToExcel(DataTable dataTable)
{
      string sData = "";
      for(int i = 0; i < dataTable.Columns.Count; i++)
      {
            sData += dataTable.Columns[i].ToString() + "\t";
      }
      sData += "\r\n";
      
      for(int i = 0; i < dataTable.Rows.Count; i++)
      {
            for(int j = 0; j < dataTable.Columns.Count; j++)
            {
                  sData += dataTable.Rows[i][j] + "\t";
            }
            sData += "\r\n";
      }

      System.Windows.Forms.Clipboard.SetDataObject(sData);                    

      xlsExcel = new Excel.Application();
      xlsBooks = (Excel.Workbooks)m_objExcel.Workbooks;
      xlsBook = (Excel._Workbook)(m_objBooks.Add(m_objOpt));

      xlsSheets = (Excel.Sheets)m_objBook.Worksheets;
      xlsSheet = (Excel._Worksheet)(m_objSheets.get_Item(1));
      xlsRange = m_objSheet.get_Range("A1", m_objOpt);
      xlsSheet.Paste(m_objRange, false);

      // Save the workbook      
}

0
 
LVL 7

Assisted Solution

by:nisarkhan
nisarkhan earned 664 total points
ID: 20069977
0
 
LVL 14

Expert Comment

by:dfu23
ID: 20200410
Just curious,
Why was my comment not selected as one to split the points with?
0
 
LVL 1

Expert Comment

by:Computer101
ID: 20237504
Forced accept.

Computer101
EE Admin
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

How to get Spreadsheet Compare 2016 working with the 64 bit version of Office 2016
This article describes how you can use Custom Document Properties to store settings and other information in your workbook so that they will be available the next time you open the workbook.
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question