I have an Excel file with 5 worksheets and I want with c# code to open it
and when it is opened I want the sheet number 3 to be activated.
How can I do that?
Like this:
using Excel;
Excel.Application excelApp = new Excel.ApplicationClass();
// if you want to make excel visible to user, set this property to true, false by default
excelApp.Visible = true;
// open an existing workbook
string workbookPath = "c:/SomeWorkBook.xls";
Excel.Workbook excelWorkbook = excelApp.Workbooks.Open(workbookPath,
0, false, 5, "", "", false, Excel.XlPlatform.xlWindows, "",
true, false, 0, true, false, false);
// get all sheets in workbook
Excel.Sheets excelSheets = excelWorkbook.Worksheets;
// get some sheet
string currentSheet = "Sheet1";
Excel.Worksheet excelWorksheet =
(Excel.Worksheet)excelSheets.get_Item(currentSheet);
// access cell within sheet
Excel.Range excelCell =
(Excel.Range)excelWorksheet.get_Range("A1", "A1");
Hope this helps
MDSN info here
What about something like this: (untested)
//using Excel = Microsoft.Office.Interop.Excel;
Excel.ApplicationClass app = new Excel.ApplicationClass();
Excel.Workbook workbook = app.Workbooks.Open("YourFile.xls",
Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing);
Excel.Worksheet worksheet = (Excel.Worksheet)workbook.Sheets["Number 3"];
worksheet.Activate();
If wanting to present visual feedback to the User, these two statements will set the activated sheet and select the range accordingly:
Consider including the following statement immediately prior to initializing the Excel.Range...
// Set Active sheet in Excel
excelWorksheet.Activate()
Also consider the following statement immediately after initializing the Excel.Range...
// Set Active range in Excel
excelCell.Activate()
public static Workbook openExternalWorkBook(String fileName)
{
Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();
excel.Visible = false;
return excel.Workbooks.Open(fileName, false);
}
Related
For my c# app, a user will input lines of remarks which will eventually be exported to an excel sheet. These remarks will be placed in a merged column cell(column I to L). The issue I am facing is that I always see a white space when the excel sheet is open as the contents are taking up too much vertical space. Only after manually resizing the height, then I am able to view the remarks within the cell.
I have tried these following : aRange.EntireColumn.AutoFit(); and worksheet.Columns.AutoFit(); and worksheet.Rows.AutoFit();
Excel.Workbook myExcelWorkbook;
Excel.Application xlsxApp = new Excel.Application();
Excel.Worksheet xlsht = new Excel.Worksheet();
File.Copy(AppDomain.CurrentDomain.BaseDirectory + #"checklist.xlsx", AppDomain.CurrentDomain.BaseDirectory + #"checklist_duplicate.xlsx", true);
string path = AppDomain.CurrentDomain.BaseDirectory + #"checklist_duplicate.xlsx;
xlsht = xlsxApp.Application.Workbooks.Open(path).Worksheets["Acceptance"];
// open the existing excel file //
myExcelWorkbook = (Excel.Workbook)(xlsxApp.Workbooks._Open(path, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Excel.XlSaveAsAccessMode.xlNoChange, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing));
xlsht.Cells[13, "I"] = Savestate.one_one_one_grouping;/*Adding remarks into the merged column cells*/
How do I auto-size the height of these merged columns so these remarks can be seen?
My intention is to store a string/numeric value in work sheet object properties, that should not readable and writable to the user.
I have tried like below, but getting exception.
Excel.Worksheet ws = Globals.ThisWorkbook.Application.ActiveWorkbook.ActiveSheet;
ws._CodeName = "abc";
Is there any possibility to store custom values in work sheet object
Although your code is c# you also show a VBA tag, so here is a VBA routine to add a customproperty to a worksheet:
Option Explicit
Sub foo()
Dim WS As Worksheet
Dim sCPName As String
Dim sCPValue As String
Dim I As Long
Set WS = Worksheets("sheet1")
sCPName = "Request ID"
sCPValue = 12345
With WS.CustomProperties
For I = 1 To .Count
If .Item(I).Name = sCPName Then Exit For
Next I
If I > .Count Then
.Add sCPName, sCPValue
Else
.Item(I).Value = sCPValue
End If
End With
End Sub
ws._CodeName is a ReadOnly property (MSDN Reference). Thus, you can only read it, and not overwrite it.
However, if you want to change the name of a worksheet, this is a possible scenario:
using System;
using Excel = Microsoft.Office.Interop.Excel;
class Startup
{
static void Main()
{
Excel.Application excelApp = new Excel.Application();
Excel.Workbook wkbReport = Open(excelApp, #"C:\Testing.xlsx");
Excel.Worksheet ws = wkbReport.Worksheets[1];
Console.WriteLine(ws._CodeName);
ws.Name = "ABC";
Console.WriteLine("END");
excelApp.Visible = true;
}
public static Excel.Workbook Open(Excel.Application excelInstance, string fileName, bool readOnly = false, bool editable = true, bool updateLinks = true)
{
Excel.Workbook book = excelInstance.Workbooks.Open(
fileName, updateLinks, readOnly,
Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, editable, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing);
return book;
}
}
This is how it would look like:
I have an excel sheet which contains previously some values with some formatting. I would like to add a new row programmatically using c#. How can I apply styling of previous row to this newly added row during insert operation?
The code that I am using is as below:
Application excelApp = new Application();
Workbooks workBooks = excelApp.Workbooks;
Workbook workBook = excelApp.Workbooks.Open(fileName, Type.Missing, false, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, true, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
int nTotalSheets = workBook.Worksheets.Count;
Worksheet workSheet = workBook.Worksheets[1];
int nColumns = workSheet.UsedRange.Columns.Count;
int nRows = workSheet.UsedRange.Rows.Count;
workSheet.Cells[nRows + 1, 1] = "Test";
Range excelRange = workSheet.UsedRange;
workBook.Close(true, fileName, false);
Marshal.ReleaseComObject(workBook);
Found the answer here
I was looking for below code:
workSheet.Cells[nRows + 1, 3] = DateTime.Today;
Range sourceRange = workSheet.Cells[nRows, 3];
sourceRange.Copy(Type.Missing);
Range destinationRange = workSheet.Cells[nRows + 1, 3];
destinationRange.PasteSpecial(XlPasteType.xlPasteFormats, XlPasteSpecialOperation.xlPasteSpecialOperationNone, false, false);
destinationRange.PasteSpecial(XlPasteType.xlPasteFormulas, XlPasteSpecialOperation.xlPasteSpecialOperationNone, false, false);
The last line is for copying formulas.
I need to create one method that it's able to copy an excel sheet and then, paste it on a new excel sheet in the same workbook, but it's necessary to copy the formatting as well.
I found several codes, but all of them not copy the formatting, only the text.
below the code that I wrote:
// Opening Excel File
Microsoft.Office.Interop.Excel.Application excel = null;
excel = new Microsoft.Office.Interop.Excel.Application();
Microsoft.Office.Interop.Excel.Workbook workbook = excel.Workbooks.Open(file, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
Microsoft.Office.Interop.Excel.Sheets sheets = workbook.Worksheets;
Microsoft.Office.Interop.Excel.Worksheet sheet = (Microsoft.Office.Interop.Excel.Worksheet)sheets.get_Item(1);
Microsoft.Office.Interop.Excel.Worksheet sheet2 = (Microsoft.Office.Interop.Excel.Worksheet)sheets.get_Item(2);
// Copy the source sheet
Object defaultArg = Type.Missing;
sheet = (Worksheet)workbook.Sheets[1];
sheet.UsedRange.Copy(defaultArg);
// Paste on destination sheet
sheet2.UsedRange._PasteSpecial(XlPasteType.xlPasteValues, XlPasteSpecialOperation.xlPasteSpecialOperationNone, false, false);
workbook.Save();
common.closeExcel(excel, workbook);
If anybody has any suggestion, please let me know.
Thanks,
This will make an exact copy of a sheet to a new sheet renamed as the original :
Dim x = 2
For numtimes = 1 To x
Sheet1.Copy _
After:=Sheet1
Next
I have the following code for creating and saving an excel file in c# but when it finishes, no file is created to my desktop, I can't figure out what I'm doing wrong:
Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();
xlApp.Visible = false;
Workbook wb = xlApp.Workbooks.Add(XlWBATemplate.xlWBATWorksheet);
Worksheet ws = (Worksheet)wb.Worksheets[1];
Range rangeAToC = ws.get_Range("A1", "C1");
string[] headerRow = { "GIP Id", "First Name", "Last Name"};
int indexAtRow = 0;
foreach (Range cell in rangeAToC)
{
cell.Value2 = headerRow[indexAtRow];
indexAtRow++;
}
//Save report
wb.SaveAs("C:/Users/Abdul/Desktop/GipEmployeeReport.xls", Type.Missing,
Type.Missing,Type.Missing, Type.Missing, Type.Missing, Excel.XlSaveAsAccessMode.xlNoChange,
Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
//Close out COM objects
xlApp.Workbooks.Close();
xlApp.Quit();
first thing i noticed is that your using forward slashes (/) instead of backslashes \