Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3968300
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T03:49:33+00:00 2026-05-20T03:49:33+00:00

I am using the following code to export a dataset from c# to excel.

  • 0

I am using the following code to export a dataset from c# to excel. I am getting the following output:

ABC | 1 | 2/15/2011 21:36 | Systems Analyst | Project Duration
ABC | 1 | 2/15/2011 21:36 | Systems Analyst | Skill of Team
ABC | 1 | 2/15/2011 21:36 | Systems Analyst | Process

What I would like to do, however, is output data in the following format, and add column headings. I would like to display NEGATIVE if the output for StoryCategoryID is 1 and POSITIVE if the output is 0. Please help!

Story | Story Type | Date | Project Member | Tag 1 | Tag 2 | Tag 3

ABC | Negative | 2/15/2011 21:36 | Project Duration | Skill of Team | Process

Code:

protected void btnExcelExport_Click(object sender, EventArgs e)
{

        string sql = null;
        string data = null;
        //string path = save_as.Text;

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

        //connectionString = "data source=servername;initial catalog=databasename;user id=username;password=password;";
        SqlConnection cnn = new SqlConnection(GetConnectionString());
        cnn.Open();
        sql = "SELECT s.Story, s.StoryCategoryID, s.CreationDate, m.CompanyRole, af.Name FROM Story s INNER JOIN ProjectIterationMember pm ON pm.ProjectIterationMemberID = s.ProjectIterationMemberID INNER JOIN Iterations i ON i.ProjectIterationID = pm.ProjectIterationID INNER JOIN Member m ON m.MemberID = pm.MemberID INNER JOIN ProjectStoryFactors psf ON psf.StoryID = s.StoryID INNER JOIN AgileFactors af ON af.AgileFactorID = psf.AgileFactorID WHERE i.ProjectID = '" + proj_id + "'";
        SqlDataAdapter dscmd = new SqlDataAdapter(sql, cnn);
        DataSet ds = new DataSet();
        dscmd.Fill(ds);

        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("excelDocument" + DateTime.Now.Ticks + ".xls", Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);
        xlWorkBook.Close(true, misValue, misValue);
        xlApp.Quit();

        releaseObject(xlWorkSheet);
        releaseObject(xlWorkBook);
        releaseObject(xlApp);

        //MessageBox.Show("Excel file created , you can find the file c:\\csharp.net-informations.xls");
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T03:49:33+00:00Added an answer on May 20, 2026 at 3:49 am

    First of all, performance-wise, it’s a lot faster to write to a range in Excel then to write cell by cell. You can do this by creating a 2-dimensional array from the data in your table and write it directly to a range.

    About the headers: Just write the text you want in the first row of Excel. You can even set the AutoFilter if you like. When writing your data, you start from row 2.

    For the change of the value to “Positive” or “Negative”: do this before starting to write to excel. Just change the values in the source (the array of another datatable if you like).

    //Create 2-dimensional array with the data from the datatable
    DataTable dt = ds.Tables[0];
    string[,] arrValues = new string[dt.Rows.Count, dt.Columns.Count];
    
    for (int i = 0; i < dt.Rows.Count; i++)
    {
        for (int j = 0; j < dt.Columns.Count; j++)
        {
            arrValues[i, j] = dt.Rows[i].ItemArray[j].ToString();
        }
    }
    
    //Change the data from the column StoryCategoryID here
    //Just loop through the items in the correct column of the array
    //and check whether it's "0" or "1"
    
    //Add headers
    for (int i = 0; i < dt.Columns.Count; i++)
    {
        xlWorkSheet.Cells[1, i + 1] = dt.Columns[i].ColumnName;
        //Or any name you like 
    }
    
    //Create range (start at row 2 because of header-row)
    Range xlRange = (Range)xlWorkSheet.Cells[2, 1];
    xlRange = xlRange.Resize[dt.Rows.Count, dt.Columns.Count];
    
    //Fill range
    xlRange.Value = arrValues;
    
    //Format document
    xlWorkSheet.EnableAutoFilter = true;
    xlWorkSheet.Cells.AutoFilter(1);
    xlWorkSheet.Range["A1", "A1"].EntireRow.Font.Bold = true;
    xlWorkSheet.Columns.AutoFit();
    

    The reason why I use a range in stead of passing the values cell by cell is due to performance. For about 1500 records (15 coloumns) it took about 7 or 8 minutes to create an Excel-file, when doing it this way (with a range) it took about 5 or 10 seconds.

    (This was some VB.Net-code I’ve converted out of my mind, so maybe there could be some “misValue” you have to pass with other parameters, but you get the picture, I think.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using the following code to export a data set to an Excel
I'm using the following code to query a database from my jsp, but I'd
I'm using the following code within the JCProperty class to retrieve data from a
I am using a PHP script to export data from MySQL into Excel. The
I need to count the number of rows returned from database.By using following code
Using the following code I get a nice formatted string: Request.QueryString.ToString Gives me something
I've been using the following code to open Office Documents, PDF, etc. on my
I am using following PHP code to connect to MS Access database: $odb_conn =
I am currently using the following code to create a web request: Dim myRequest
I'm using the following code, using the SharpZipLib library, to add files to a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.