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:
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.
3 10435
i reocrded a macro in excel to create the following: -
Cells.Select
-
ActiveWorkbook.Worksheets("Report").Sort.SortFields.Clear
-
ActiveWorkbook.Worksheets("Report").Sort.SortFields.Add Key:=Range("F2:F57") _
-
, SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
-
ActiveWorkbook.Worksheets("Report").Sort.SortFields.Add Key:=Range("B2:B57") _
-
, SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
-
With ActiveWorkbook.Worksheets("Report").Sort
-
.SetRange Range("A1:G57")
-
.Header = xlYes
-
.MatchCase = False
-
.Orientation = xlTopToBottom
-
.SortMethod = xlPinYin
-
.Apply
-
End With
-
i just need assistance converting it to C#.
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. - using Excel = Microsoft.Office.Interop.Excel;
- // Method that returns the index of the specified worksheet name
-
private int getSheetIndex(string sheetName, Excel.Sheets shs)
-
{
-
int i = 0;
-
foreach (Excel.Worksheet sh in shs)
-
{
-
if (sheetName == sh.Name)
-
{
-
return i + 1;
-
}
-
i += 1;
-
}
-
return 0;
-
}
-
-
void Button1Click(object sender, EventArgs e)
-
{
-
Excel.Application oXls; // Excel Object
-
oXls = new Excel.Application();
-
Excel.Workbook oWBook; // workbook Object
-
-
string excelName = "C:\\sample.xlsx";
-
-
oXls = new Excel.Application();
-
oXls.Visible = true; // Show Excel window for confirmation
-
-
// Open Excel file
-
oWBook = (Excel.Workbook)(oXls.Workbooks.Open(
-
excelName, // Excel file name
-
Type.Missing, // (Can be omitted)UpdateLinks (0 / 1 / 2 / 3)
-
Type.Missing, // (Can be omitted)ReadOnly (True / False )
-
Type.Missing, // (Can be omitted)Format
-
// 1:Tab / 2:Comma (,) / 3:Space / 4:Semicolon (;)
-
// 5:Non / 6:Characters specified by Parameter Delimiter
-
Type.Missing, // (Can be omitted)Password
-
Type.Missing, // (Can be omitted)WriteResPassword
-
Type.Missing, // (Can be omitted)IgnoreReadOnlyRecommended
-
Type.Missing, // (Can be omitted)Origin
-
Type.Missing, // (Can be omitted)Delimiter
-
Type.Missing, // (Can be omitted)Editable
-
Type.Missing, // (Can be omitted)Notify
-
Type.Missing, // (Can be omitted)Converter
-
Type.Missing, // (Can be omitted)AddToMru
-
Type.Missing, // (Can be omitted)Local
-
Type.Missing // (Can be omitted)CorruptLoad
-
));
-
// Get a Worksheet object from the given worksheet name
-
string sheetName = "Report";
-
Excel.Worksheet oSheet; // Worksheet Object
-
oSheet = (Excel.Worksheet)oWBook.Sheets[
-
getSheetIndex(sheetName, oWBook.Sheets)];
-
-
// Set sort properties
-
oSheet.Sort.SetRange(oSheet.Range["A1", "E7"]);
-
oSheet.Sort.Header = Excel.XlYesNoGuess.xlYes;
-
oSheet.Sort.SortFields.Add(oSheet.Range["B1", "B7"], Excel.XlSortOn.xlSortOnValues,
-
Excel.XlSortOrder.xlAscending);
-
-
// Sort worksheet
-
oSheet.Sort.Apply();
-
:
-
:
-
:
-
-
oSheet=null;
-
oWBook=null;
-
oXls=null;
-
}
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
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,...
|
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...
|
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...
|
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...
| |