473,395 Members | 2,443 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

C# sort excel worksheet

epots9
1,351 Expert 1GB
I'm still learning C# so forgive me if this question seems "noobish".

I've created an excel template using C#, what it does is connects to our ERP system and gets the specific values wanted. All this is done through an actions pane control. The data comes through fine and appears where and how i want it, but thats not the problem.

There are 7 columns that get filled (rows vary based on results from db query). One department needs the file to be sorted by job # (column A1) then work order # (column B1)...while the other department needs it by shipping date (column F1) then work order # (column B1). I'm sure after i figure out one way it would be easy enough to figure out the sort for the other.

Since all my code is in the actions pane control, to access the worksheet i do:

Expand|Select|Wrap|Line Numbers
  1. Globals.Sheet1.X
  2.  
I've tried searching but haven't been able to find anything helpful. If anyone can help me get this sorting to work that would be greatly appreciated.

Thanks in advance.
Nov 10 '08 #1
3 10435
epots9
1,351 Expert 1GB
i reocrded a macro in excel to create the following:

Expand|Select|Wrap|Line Numbers
  1. Cells.Select
  2.     ActiveWorkbook.Worksheets("Report").Sort.SortFields.Clear
  3.     ActiveWorkbook.Worksheets("Report").Sort.SortFields.Add Key:=Range("F2:F57") _
  4.         , SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
  5.     ActiveWorkbook.Worksheets("Report").Sort.SortFields.Add Key:=Range("B2:B57") _
  6.         , SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
  7.     With ActiveWorkbook.Worksheets("Report").Sort
  8.         .SetRange Range("A1:G57")
  9.         .Header = xlYes
  10.         .MatchCase = False
  11.         .Orientation = xlTopToBottom
  12.         .SortMethod = xlPinYin
  13.         .Apply
  14.     End With
  15.  
i just need assistance converting it to C#.
Nov 10 '08 #2
joedeene
583 512MB
...i just need assistance converting it to C#.
Take a look at this helpful site that insertAlias has previously pointed out. It is a great resource on the comparison between Visual Basic.net and C#.net. Here's the link: http://www.harding.edu/fmccown/vbnet...omparison.html

joedeene
Nov 10 '08 #3
SioSio
272 256MB
This is a sample that searches for the "Report" sheet of "C:\\sample.xlsx" and sorts the range (A1:E7) using the B column as the sort key.

First, add a reference from GAC. : Microsoft Office.Interop.Excel.

Click button 1 on the form to start.
Expand|Select|Wrap|Line Numbers
  1. using Excel = Microsoft.Office.Interop.Excel;
Expand|Select|Wrap|Line Numbers
  1.         // Method that returns the index of the specified worksheet name
  2.         private int getSheetIndex(string sheetName, Excel.Sheets shs)
  3.         {
  4.             int i = 0;
  5.             foreach (Excel.Worksheet sh in shs)
  6.             {
  7.                 if (sheetName == sh.Name)
  8.                 {
  9.                      return i + 1;
  10.                 }
  11.                 i += 1;
  12.             }
  13.             return 0;
  14.         }
  15.  
  16.         void Button1Click(object sender, EventArgs e)
  17.         {
  18.             Excel.Application oXls; // Excel Object
  19.             oXls = new Excel.Application();
  20.             Excel.Workbook oWBook; // workbook Object
  21.  
  22.             string excelName = "C:\\sample.xlsx";
  23.  
  24.             oXls = new Excel.Application();
  25.             oXls.Visible = true; // Show Excel window for confirmation
  26.  
  27.             // Open Excel file
  28.             oWBook = (Excel.Workbook)(oXls.Workbooks.Open(
  29.                 excelName,  // Excel file name
  30.                 Type.Missing, // (Can be omitted)UpdateLinks (0 / 1 / 2 / 3)
  31.                 Type.Missing, // (Can be omitted)ReadOnly (True / False )
  32.                 Type.Missing, // (Can be omitted)Format
  33.                 // 1:Tab / 2:Comma (,) / 3:Space / 4:Semicolon (;)
  34.                 // 5:Non / 6:Characters specified by Parameter Delimiter
  35.                 Type.Missing, // (Can be omitted)Password
  36.                 Type.Missing, // (Can be omitted)WriteResPassword
  37.                 Type.Missing, // (Can be omitted)IgnoreReadOnlyRecommended
  38.                 Type.Missing, // (Can be omitted)Origin
  39.                 Type.Missing, // (Can be omitted)Delimiter
  40.                 Type.Missing, // (Can be omitted)Editable
  41.                 Type.Missing, // (Can be omitted)Notify
  42.                 Type.Missing, // (Can be omitted)Converter
  43.                 Type.Missing, // (Can be omitted)AddToMru
  44.                 Type.Missing, // (Can be omitted)Local
  45.                 Type.Missing  // (Can be omitted)CorruptLoad
  46.             ));
  47.             // Get a Worksheet object from the given worksheet name
  48.             string sheetName = "Report";
  49.             Excel.Worksheet oSheet; // Worksheet Object
  50.             oSheet = (Excel.Worksheet)oWBook.Sheets[
  51.                 getSheetIndex(sheetName, oWBook.Sheets)];
  52.  
  53.             // Set sort properties          
  54.             oSheet.Sort.SetRange(oSheet.Range["A1", "E7"]);
  55.             oSheet.Sort.Header = Excel.XlYesNoGuess.xlYes;
  56.             oSheet.Sort.SortFields.Add(oSheet.Range["B1", "B7"], Excel.XlSortOn.xlSortOnValues,
  57.                 Excel.XlSortOrder.xlAscending);
  58.  
  59.             // Sort worksheet
  60.             oSheet.Sort.Apply();
  61. :
  62. :
  63. :
  64.  
  65.                         oSheet=null;
  66.                         oWBook=null;
  67.                         oXls=null;
  68.         }
Jul 8 '20 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

13
by: Allison Bailey | last post by:
Hi Folks, I'm a brand new Python programmer, so please point me in the right direction if this is not the best forum for this question.... I would like to open an existing MS Excel spreadsheet...
1
by: c duden | last post by:
Excel won't allow documents to be linked to a worksheet in office 2000 when using C# to create an add-in. The following code snippets are what was used in the attempts (there are more...
1
by: mail2atulmehta | last post by:
Hi, I do not know if this is right place for this, but i need some help. I have a text file, whose values are seprated by a delimiter. I want to open this file in excel, ( not import it) . I have...
0
by: dgoel | last post by:
Hi, I Have a text file & I want to open it in excel sheet ( withou importing). I have written code for it, but it is not opening exce sheet. It opens the text file, but does not create a excel...
1
by: javzxp | last post by:
Hi I'd like to use C# to open an existing Excel workbook and save each worksheet it contains into a new Excel file. The name of each new Excel file should be the name of the worksheet copied...
8
by: Sam | last post by:
Hi All, I posted this message few days ago. No one has answered so far. I just would like to know if it is possible to do or not. Even if you can not tell me how to do this, maybe you know which...
1
by: Randall Arnold | last post by:
I'm converting a vbscript program to vb.net. Witht he exception of .net idiosyncrasies, most of it is working well with the same code. My only problem is that some properties and methods are...
6
by: gumbystation | last post by:
I am looking to take data from an excel spreadsheet into a csharp application to manipulate the data in various ways. Currently, I am using VS2005 (self-taught C#) and Excel 2000. I have...
3
by: sejal17 | last post by:
hello Can any one tell me how to read multiple worksheets from a single excel file.I have stored that excel in xml file.so i want to read that xml that has multiple worksheet.And i want to store...
3
by: sejal17 | last post by:
hello Can any one tell me how to read multiple worksheets from a single excel file.I have stored that excel in xml file.so i want to read that xml that has multiple worksheet.And i want to store...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.