2012-11-05 43 views
1

我有一個與SQL Server數據庫交互的C#WPF應用程序。在應用程序中,我希望用戶能夠將某些數據從SQL導出到Excel。我試着先填充一個Datatable,然後將其導出到excel文件。一切都很好,直到我遇到了一個任務,充滿了這個SQL查詢的DataTable導出:enter image description here如何從sql導出爲格式化的excel

到的EXCEL文件是這樣的:出售 report

所以,我怎麼能進行排序的每selldetails excel文件中的一個區域? 我已經很長一段時間瞭解這個問題,請幫助我!

+1

嗨PLease看看這個http://www.codeproject.com/Articles/20228/Using-C-to-Create-an-Excel-Document –

+0

感謝您的鏈接,但它不是我想要的。我想在Excel文件中對相同的ID進行分組,以便所有與id selldetails相關的信息都放在一個區域中。 –

+1

是的,你必須以編程方式進行 –

回答

3

萬一如果有人需要以類比的方式解決問題,我用這段代碼實現了目標:

 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; 
相關問題