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

  • Home
  • SEARCH
  • 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 8794531
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T23:15:52+00:00 2026-06-13T23:15:52+00:00

I have a C# WPF application which interacts with SQL Server Database. In application

  • 0

I have a C# WPF application which interacts with SQL Server Database. In application I want user be able to export some kind of data from SQL to Excel. I’m trying firstly to fill a Datatable and then export it to excel file. Everything was fine, untill I faced up with a task to export a datatable filled with this sql query:enter image description here

to a formatted excel file like this:
report

So how can I sort selldetails of every sell in one region in excel file?
It’s been a long time I’m puzzling my mind over this problem, please, help me!

  • 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-06-13T23:15:53+00:00Added an answer on June 13, 2026 at 11:15 pm

    In case if someone needs to solve a problem in analogy I achieved the goal with this piece of code:

            int currentXLLine = 1; // line in xls file to start
            int currentId = -1;    // current sellID  
            int detailCount = 1;
            int beginSumLine = currentXLLine+3;
            for (int i = 0; i < dt.Rows.Count; i++) 
            {
                if ((currentId != (int)dt.Rows[i][0])) 
                {
    
                    if (i != 0)
                    {
                        cell = oSheet.Cells[currentXLLine+1, 7];
                        cell.Font.Bold = true;
                        cell.HorizontalAlignment = ExcelApp.Constants.xlRight;
                        cell.Value = "OVERALL:";
                        cell = oSheet.Cells[currentXLLine + 1, 8];
                        cell.Formula = "=SUM(H"+beginSumLine.ToString()+":H" + currentXLLine.ToString() + ")";
                        cell.Font.Bold = true;
                        cell.HorizontalAlignment = ExcelApp.Constants.xlRight;
    
                        cell = oSheet.Columns[7];
                        Past.AutoFitColumn(oSheet, 7);
                        cell.NumberFormat = "# ##0.00";
                        cell = oSheet.Columns[8];
                        Past.AutoFitColumn(oSheet, 8);
                        cell.NumberFormat = "# ##0.00";
                        cell.HorizontalAlignment = ExcelApp.Constants.xlCenter;
    
                        currentXLLine += 3;
                        detailCount = 1;
    
                    }
    
                    oRange = oSheet.get_Range("B" + currentXLLine.ToString(), "C" + currentXLLine.ToString());
                    oRange.Merge(Type.Missing);
                    oRange.BorderAround(ExcelApp.XlLineStyle.xlContinuous, ExcelApp.XlBorderWeight.xlThin, Microsoft.Office.Interop.Excel.XlColorIndex.xlColorIndexAutomatic, Missing.Value);
                    oRange.HorizontalAlignment = ExcelApp.Constants.xlRight;
                    oRange.VerticalAlignment = ExcelApp.Constants.xlCenter;
                    oRange.EntireRow.Font.Bold = false;
                    oSheet.Cells[currentXLLine, 2] = "SellDate";
    
                    oRange = oSheet.get_Range("D" + currentXLLine.ToString(), "E" + currentXLLine.ToString());
                    oRange.Merge(Type.Missing);
                    oRange.BorderAround(ExcelApp.XlLineStyle.xlContinuous, ExcelApp.XlBorderWeight.xlThin, Microsoft.Office.Interop.Excel.XlColorIndex.xlColorIndexAutomatic, Missing.Value);
                    oRange.HorizontalAlignment = ExcelApp.Constants.xlLeft;
                    oRange.VerticalAlignment = ExcelApp.Constants.xlCenter;
                    oRange.EntireRow.Font.Bold = false;
                    oSheet.Cells[currentXLLine, 4] = dt.Rows[i][3].ToString();
                    beginSumLine = currentXLLine + 3;
    
                    currentXLLine += 2;
    
    
                    oRange = oSheet.get_Range("C" + currentXLLine.ToString(), "E" + currentXLLine.ToString());
                    oRange.Merge(Type.Missing);
                    oRange.BorderAround(ExcelApp.XlLineStyle.xlContinuous, ExcelApp.XlBorderWeight.xlThin, Microsoft.Office.Interop.Excel.XlColorIndex.xlColorIndexAutomatic, Missing.Value);
                    oRange.HorizontalAlignment = ExcelApp.Constants.xlCenter;
                    oRange.VerticalAlignment = ExcelApp.Constants.xlCenter;
                    oRange.EntireRow.Font.Bold = true;
    
                    oRange = oSheet.get_Range("B" + currentXLLine.ToString(), "B" + currentXLLine.ToString());
                    oRange.Merge(Type.Missing);
                    oRange.BorderAround(ExcelApp.XlLineStyle.xlContinuous, ExcelApp.XlBorderWeight.xlThin, Microsoft.Office.Interop.Excel.XlColorIndex.xlColorIndexAutomatic, Missing.Value);
                    oRange.HorizontalAlignment = ExcelApp.Constants.xlCenter;
                    oRange.VerticalAlignment = ExcelApp.Constants.xlCenter;
                    oRange.EntireRow.Font.Bold = true;
    
                    oRange = oSheet.get_Range("F" + currentXLLine.ToString(), "F" + currentXLLine.ToString());
                    oRange.Merge(Type.Missing);
                    oRange.BorderAround(ExcelApp.XlLineStyle.xlContinuous, ExcelApp.XlBorderWeight.xlThin, Microsoft.Office.Interop.Excel.XlColorIndex.xlColorIndexAutomatic, Missing.Value);
                    oRange.HorizontalAlignment = ExcelApp.Constants.xlCenter;
                    oRange.VerticalAlignment = ExcelApp.Constants.xlCenter;
                    oRange.EntireRow.Font.Bold = true;
    
                    oRange = oSheet.get_Range("G" + currentXLLine.ToString(), "G" + currentXLLine.ToString());
                    oRange.Merge(Type.Missing);
                    oRange.BorderAround(ExcelApp.XlLineStyle.xlContinuous, ExcelApp.XlBorderWeight.xlThin, Microsoft.Office.Interop.Excel.XlColorIndex.xlColorIndexAutomatic, Missing.Value);
                    oRange.HorizontalAlignment = ExcelApp.Constants.xlCenter;
                    oRange.VerticalAlignment = ExcelApp.Constants.xlCenter;
                    oRange.EntireRow.Font.Bold = true;
    
                    oRange = oSheet.get_Range("H" + currentXLLine.ToString(), "H" + currentXLLine.ToString());
                    oRange.Merge(Type.Missing);
                    oRange.BorderAround(ExcelApp.XlLineStyle.xlContinuous, ExcelApp.XlBorderWeight.xlThin, Microsoft.Office.Interop.Excel.XlColorIndex.xlColorIndexAutomatic, Missing.Value);
                    oRange.HorizontalAlignment = ExcelApp.Constants.xlCenter;
                    oRange.VerticalAlignment = ExcelApp.Constants.xlCenter;
                    oRange.EntireRow.Font.Bold = true;
    
                    oSheet.Cells[currentXLLine, 2] = "№";
                    oSheet.Cells[currentXLLine, 3] = "ProductName";
                    oSheet.Cells[currentXLLine, 6] = "Quantity";
                    oSheet.Cells[currentXLLine, 7] = "Price";
                    oSheet.Cells[currentXLLine, 8] = "Sum";
                    currentXLLine += 1;
    
                }
    
            oSheet.Cells[currentXLLine, 2] = (detailCount).ToString(); //rowView.Row["Ассортимент"].ToString();
            cell = oSheet.Cells[currentXLLine, 2];
            cell.BorderAround(ExcelApp.XlLineStyle.xlContinuous, ExcelApp.XlBorderWeight.xlThin, Microsoft.Office.Interop.Excel.XlColorIndex.xlColorIndexAutomatic, Missing.Value);
    
            oSheet.Cells[currentXLLine, 3] = (string)dt.Rows[i][4];
            cell = oSheet.Cells[currentXLLine, 3];
            cell.BorderAround(ExcelApp.XlLineStyle.xlContinuous, ExcelApp.XlBorderWeight.xlThin, Microsoft.Office.Interop.Excel.XlColorIndex.xlColorIndexAutomatic, Missing.Value);
            oRange = oSheet.get_Range("C" + (currentXLLine).ToString(), "E" + (currentXLLine).ToString());
            oRange.Merge(Type.Missing);
            oRange.BorderAround(ExcelApp.XlLineStyle.xlContinuous, ExcelApp.XlBorderWeight.xlThin, Microsoft.Office.Interop.Excel.XlColorIndex.xlColorIndexAutomatic, Missing.Value);
            oSheet.Cells[currentXLLine, 6] = dt.Rows[i][5].ToString();
            cell = oSheet.Cells[currentXLLine, 6];
            cell.BorderAround(ExcelApp.XlLineStyle.xlContinuous, ExcelApp.XlBorderWeight.xlThin, Microsoft.Office.Interop.Excel.XlColorIndex.xlColorIndexAutomatic, Missing.Value);
            oSheet.Cells[currentXLLine, 7] = dt.Rows[i][6].ToString();
            cell = oSheet.Cells[currentXLLine, 7];
            cell.BorderAround(ExcelApp.XlLineStyle.xlContinuous, ExcelApp.XlBorderWeight.xlThin, Microsoft.Office.Interop.Excel.XlColorIndex.xlColorIndexAutomatic, Missing.Value);
            oSheet.Cells[currentXLLine, 8] = Convert.ToInt16(dt.Rows[i][5])*Convert.ToDouble(dt.Rows[i][6]);
            cell = oSheet.Cells[currentXLLine, 8];
            cell.BorderAround(ExcelApp.XlLineStyle.xlContinuous, ExcelApp.XlBorderWeight.xlThin, Microsoft.Office.Interop.Excel.XlColorIndex.xlColorIndexAutomatic, Missing.Value);
            detailCount += 1;
            currentXLLine += 1;
            currentId = (int)dt.Rows[i][0];
    
        }
    
    
    cell = oSheet.Cells[currentXLLine + 1, 7];
      cell.Font.Bold = true;
      cell.HorizontalAlignment = ExcelApp.Constants.xlRight;
      cell.Value = "OVERALL:";
      cell = oSheet.Cells[currentXLLine + 1, 8];
      cell.Formula = "=SUM(H" + beginSumLine.ToString() + ":H" + currentXLLine.ToString() + ")";
      cell.Font.Bold = true;
      cell.HorizontalAlignment = ExcelApp.Constants.xlRight;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a WPF C# multi-user applicaton which interacts with Sql Server Express database.
I have C# WPF application which reads data from database then does some work.
I have WPF application in which user enters some text in rich text box(rtb),
I have a WPF Application which allows user to enter production Data. For that
Please help! Background info I have a WPF application which accesses a SQL Server
I have a WPF application which connects to a remote database over internet and
I have created a wpf application into which I have added a user control
In my WPF application I have a Canvas in which I do some drawing.
I have a C# application of which some parts are written using WPF (which
I am developing an application in C# WPF which will have Client-Server architecture (Client

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.