I am attempting to add a worksheet to my existing Excel instance using C#, but I get an error of
Object not set to an instance of an object
The line that throws the code is the one that begins with newSheet =
I am not sure why this is being thrown as I declare my variable newSheet and I have instantiated xlApp ---- What do I need to alter in order for this syntax to work as expected?
public static void AddNewSheet()
{
Excel.Application xlApp;
xlApp = new Excel.Application();
Excel.Worksheet Jobs, Estimates;
string filePath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + #"\Test.xlsx";
var xlWorkBook = xlApp.Workbooks.Open(filePath);
Excel.Worksheet newSheet;
//Add a new worksheet to the workbook
newSheet = (Excel.Worksheet)xlApp.Worksheets.Add(System.Reflection.Missing.Value,
xlWorkBook.Worksheets[xlWorkBook.Worksheets.Count],
System.Reflection.Missing.Value,
System.Reflection.Missing.Value);
}
EDIT
Additional Code - to hopefully assist with adding worksheets to already created workbook.
namespace GenerateExcelWorkbookData
{
public partial class Form1 : Form
{
public static Excel.Application xlApp;
public static Excel.Workbook xlWorkBook;
public static Excel.Worksheet xlWorkSheet;
public static Excel.Worksheet newSeet;
Main()
{
//Query Excel To Get DataTable Here
//Excel Info
xlApp = new Excel.Application();
xlWorkBook = xlApp.Workbooks.Add(misValue);
xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
CreateSubWorksheets();
}
public static void CreateSubWorksheets()
{
string sheetNameprefix = "SS";
Excel.Worksheet sheet1;
sheet1 = xlApp.ActiveWorkbook.Worksheets.Item["Newly Added"];
Excel.Range range = sheet1.Range["A1"].CurrentRegion;
object OriginalData = range.Value;
string OriginalAddress = range.Address;
while (range.Cells[2, 1].Value != null && range.Cells[2, 1].Value != "")
{
range.AutoFilter(1, range.Cells[2, 1].Value);
newSeet = (Excel.Worksheet)xlApp.Worksheets.Add(System.Reflection.Missing.Value,
xlWorkBook.Worksheets[xlWorkBook.Worksheets.Count],
System.Reflection.Missing.Value,
System.Reflection.Missing.Value);
//Do more stuff here
}
}
}
}
Try this...
Microsoft.Office.Interop.Excel.Application Excel = null;
Microsoft.Office.Interop.Excel._Workbook oWB = null;
Microsoft.Office.Interop.Excel._Worksheet oSheet = null;
try
{
Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();
if (xlApp == null)
{
return false;
}
Excel.Workbook xlWorkBook;
Excel.Worksheet xlWorkSheet;
object misValue = System.Reflection.Missing.Value;
xlWorkBook = xlApp.Workbooks.Add(misValue);
xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.Add();
xlWorkSheet.Name = "Details";
}
Related
i'm trying to insert some data to my excel sheet.
My main code returns true/false statement and i need to save that statements to my Registers.xls file.
How do i read and write data from same excel. (Sorry for poor english)
static void Main(string[] args)
{
Excel.Application xlApp;
Excel.Workbook xlWorkbook;
Excel.Worksheet xlWorksheet;
Excel.Range range;
xlApp = new Excel.Application();
xlWorkbook = xlApp.Workbooks.Open(#"E:\VScode\Registers.xls");
xlWorksheet = (Excel.Worksheet)xlWorkbook.Worksheets.get_Item(1);
range = xlWorksheet.UsedRange;
int cl = range.Rows.Count;
string result;
string strRegist;
for (int i = 1; i <= cl;i++)
{
strRegist = (string)(range.Cells[i,3] as Excel.Range).Value2;
result = Convert.ToString(Program.CheckRegister(strRegist));
//Insert result to my sheet
Console.WriteLine(result);
}
xlWorkbook.Close(true, null, null);
xlApp.Quit();
Marshal.ReleaseComObject(xlWorksheet);
Marshal.ReleaseComObject(xlWorkbook);
Marshal.ReleaseComObject(xlApp);
Console.WriteLine();
Console.ReadLine();
}
You could add your data into a datatable and then convert that to an excelsheet and adding that sheet into w/e workbook you want.
using OfficeOpenXml;
using System.Data;
using System.IO;
void SaveTableAsExcelSheet(DataTable dt, string filePath)
{
var fi = new FileInfo(filePath);
using (ExcelPackage pck = new ExcelPackage(fi))
{
ExcelWorksheet ws = pck.Workbook.Worksheets.Add(dt.TableName);
ws.Cells["A1"].LoadFromDataTable(dt, true);
pck.Save();
}
}
I am unable to select range using excel interop in C#. Please let me know what am I doing wrong here. I am very new to excel interop
static void Main(string[] args)
{
Excel.Application xlApp = new Excel.Application();
Excel.Workbook xlWorkbook = xlApp.Workbooks.Open("D:\\s1.xlsx");
Excel.Range xlTestRange;
Excel._Worksheet xlWorksheet = xlWorkbook.Sheets[2];
xlTestRange = xlWorksheet.UsedRange;
xlTestRange.Select();
xlWorkbook.Save();
xlWorkbook.Close();
xlApp.UserControl = true;
xlApp.Quit();
}
The following code worked for me in LINQPad:
var xlApp = new Excel.Application();
var xlWorkbook = xlApp.Workbooks.Open(#"path-to-excel-file");
var xlWorksheet = (Excel.Worksheet)xlWorkbook.Sheets[1];
var xlTestRange = xlWorksheet.UsedRange;
xlTestRange.Select();
xlWorkbook.Save();
xlWorkbook.Close();
xlApp.Quit();
I have an Excel work book that I am adding work sheets using a loop. In the sample code below I am using a fixed array but the real code uses a database to get the names from and there can be one or many (less than 10 though).
The issue that I am having (with my code) is the worksheets get added in the reverse order of the array. So the excel sheet will open with the tabs ordered like this 1103 1102 1101 Sheet1. I know I can reorder the results from the database but that feels like a hack and I am sure there is a way to accomplish this in code. What needs to be done to set the order of tabs correctly inside my loop?
private static void SetWorksheet()
{
Excel.Application xlApp;
Excel.Workbook xlWorkBook;
Excel.Worksheet xlWorkSheet;
object misValue = System.Reflection.Missing.Value;
xlApp = new Excel.Application();
xlApp.Visible = true;
xlWorkBook = xlApp.Workbooks.Add(1);
string[] storeArray = { "1101", "1102", "1103" };
foreach (string s in storeArray)
{
xlWorkBook.Worksheets.Add();
xlWorkBook.Worksheets.Move(After: xlWorkBook.Sheets[xlWorkBook.Sheets.Count]);
xlWorkSheet = (Excel.Worksheet)xlWorkBook.Sheets[1];
xlWorkSheet.Name = s;
releaseObject(xlWorkSheet);
}
releaseObject(xlWorkBook);
releaseObject(xlApp);
}
You can add new worksheet after the last sheet by put parameter 'After' in Add function.
xlWorkBook.Worksheets.Add(After: xlWorkBook.Sheets[xlWorkBook.Sheets.Count]);
Then you do not need to move it by using Move function.
And in the line
xlWorkSheet = (Excel.Worksheet)xlWorkBook.Sheets[1];
It always get first sheet to change the name.
You need to change '1' to xlWorkBook.Sheets.Count
xlWorkSheet = (Excel.Worksheet)xlWorkBook.Sheets[xlWorkBook.Sheets.Count];
Finally your code should be as below
private static void SetWorksheet()
{
Excel.Application xlApp;
Excel.Workbook xlWorkBook;
Excel.Worksheet xlWorkSheet;
object misValue = System.Reflection.Missing.Value;
xlApp = new Excel.Application();
xlApp.Visible = true;
xlWorkBook = xlApp.Workbooks.Add(1);
string[] storeArray = { "1101", "1102", "1103" };
foreach (string s in storeArray)
{
xlWorkBook.Worksheets.Add(After: xlWorkBook.Sheets[xlWorkBook.Sheets.Count]);
//xlWorkBook.ActiveSheet(After: xlWorkBook.Sheets[xlWorkBook.Sheets.Count]);
xlWorkSheet = (Excel.Worksheet)xlWorkBook.Sheets[xlWorkBook.Sheets.Count];
xlWorkSheet.Name = s;
releaseObject(xlWorkSheet);
}
releaseObject(xlWorkBook);
releaseObject(xlApp);
}
you can add a new work sheet after 3 sheet in default in excel but you can add more then one sheet by using below code:
Microsoft.Office.Interop.Excel.Worksheet sheet1 = (Microsoft.Office.Interop.Excel.Worksheet)excelApp.Sheets.get_Item(1);
Microsoft.Office.Interop.Excel.Worksheet sheet4 = (Microsoft.Office.Interop.Excel.Worksheet)excelApp.Sheets.Add(After: wb.Sheets[3]);
I need to insert text in 5th row ist column and another text in 5th row 2nd column... How do I do this? Below is the code am using and get out of range exception:
Excel.Application xlApp;
Excel.Workbook xlWorkBook;
Excel.Worksheet xlWorkSheet;
Excel.Worksheet xlWorkSheet2;
Excel.Worksheet xlWorkSheet3;
Excel.Range oRange;
xlApp = new Excel.Application();
xlWorkBook = xlApp.Workbooks.Add(misValue);
xlWorkSheet2 = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
xlWorkSheet2.Name = "The chart sheet";
xlWorkSheet2.Cells[5, 1] = "First Name";
xlWorkSheet2.Cells[5,2] = "Last Name";
Before the cell insertion above you have to add a worksheet
Excel.Application excelApplication = new Excel.Application();
Excel.Workbook excelWorkBook = excelApplication.Workbooks.Add();
Excel.Worksheet wkSheetData = excelWorkBook.ActiveSheet;
excelApplication.Cells[5, 2] = "TextField";
I hope this helps
I want to ask you guys, how to change color of a row to red in Excel table if the cell 1 isn't null.
XX YY ZZ
-----------------
aa bb cc
aa1 bb1 cc1
aa2 cc2
aa3 bb3 cc3
aa4
Excel.Application xlApp;
Excel. Workbook xlWorkBook;
Excel.Worksheet xlWorkSheet;
I'm very thankfull
Give this a shot, I have tested it and it works:
Excel.Application application = new Excel.Application();
Excel.Workbook workbook = application.Workbooks.Open(#"C:\Test\Whatever.xlsx");
Excel.Worksheet worksheet = workbook.ActiveSheet;
Excel.Range usedRange = worksheet.UsedRange;
Excel.Range rows = usedRange.Rows;
int count = 0;
foreach (Excel.Range row in rows)
{
if (count > 0)
{
Excel.Range firstCell = row.Cells[1];
string firstCellValue = firstCell.Value as String;
if (!string.IsNullOrEmpty(firstCellValue))
{
row.Interior.Color = System.Drawing.Color.Red;
}
}
count++;
}
workbook.Save();
workbook.Close();
application.Quit();
Marshal.ReleaseComObject(application);
Excel.Application xlAppToExport = new Excel.Application();
xlAppToExport.Workbooks.Add("");
Excel.Worksheet xlWorkSheetToExport = default(Excel.Worksheet);
xlWorkSheetToExport = (Excel.Worksheet)xlAppToExport.Sheets["Sheet1"];
xlWorkSheetToExport.Range["A5:F5"].EntireRow.Interior.Color = System.Drawing.Color.Gray;