how to detect an empty worksheet - c#

I am attempting to use the ExcelWriter class to read the contents of an uploaded Excel file in a way where the code will process the data in any of the workbook's worksheets.
So something like this:
foreach (var worksheet in workbook.Worksheets)
{
var area = worksheet.PopulatedCells;
if (area == null)
break;
...do stuff with the cells in the area...
}
The problem is that when I call Worksheet.PopulatedCells I get an exception thrown saying that there aren't any populated cells when the worksheet is empty rather than returning me null.
Is there some way to detect if a Worksheet is empty?

Related

c# - (Excel interop)How can I copy one worksheet to another workbook in VSTO?

I have searched a lot from the internet, but unfortunately, none of them worked.
How to copy sheets from a workbook to another workbook
,Cutting and Pasting Cell Values in Excel using C#,Error: Pastespecial method of range class failed
.
The thing is if I created the worksheet in the same workbook, the code works perfectly fine.
But if I created a new workbook, then the Paste would not work.
//copy function: copy to a new worksheet in the same workbook
var copyFromRange = oldSheet.Range[
(Excel.Range)oldSheet.Cells[1, 1],
oldSheet.Cells[firstPackageStartLine - 1, lastColIgnoreFormulas]];
copyFromRange.Copy(Type.Missing);
var copyHeaderRange = newSheet.Range[(Excel.Range)newSheet.Cells[1, 1], newSheet.Cells[firstPackageStartLine - 1, lastColIgnoreFormulas]];
copyHeaderRange.PasteSpecial(Excel.XlPasteType.xlPasteAll, Excel.XlPasteSpecialOperation.xlPasteSpecialOperationNone, false, false);
//Copy to a new workbook, won't work
var excel = new Excel.Application();
var newWorkbook = excel.Workbooks.Add(System.Reflection.Missing.Value);
var newSheet = (Excel.Worksheet)newWorkbook.Worksheets.Add();
newSheet.Name = "sheetName";
//copy function, same as above. But this won't work, PasteSpecial would throw null reference error.
//If I changed the copyHeaderRange to below, then it will throw: PasteSpecial method of Range class failed.
var copyHeaderRange = (Excel.Range)newSheet.Cells[1, 1];
//If the copyRange is defined as below, it will throw null reference error while pastespecial
var copyHeaderRange = newSheet.Range[(Excel.Range)newSheet.Cells[1, 1], newSheet.Cells[firstPackageStartLine - 1, lastColIgnoreFormulas]];
I have also tried open a new workbook instead of starting a new one, won't work . Tried activate the worksheet before copying, won't work either.
Any idea why this happens?
If there are any merged cells in the copy range -- even if they do not span beyond that range's coordinate cells -- and you're creating a new instance of Excel Application class, I think this error is expected.
Rather than newing your excel instance, can you modify to use the same instance of Excel (untested):
var newWorkbook = oldSheet.Application.Workbooks.Add(System.Reflection.Missing.Value);
var newSheet = (Excel.Worksheet)newWorkbook.Worksheets.Add();
newSheet.Name = "sheetName";

Excel Read Value with Formulas to Lookup Failing

Workbook SourceFile = oXLSrc.Workbooks.Open(Path.GetFileName(fileName), true);
TestSheet.get_Range("E33", Type.Missing);
The Sheet have a value and it's a ComboBox and data validation. This ComboBox values are coming from another worksheet. When I open the Excel in C#, ALL values are NULL. But when I open it in regular Excel it's working well and data is there. What am I missing?
Just based on your code above, you're not assigning the result of get_Range() to a variable. I expect to see something like...
private Workbook m_Workbook; // The currently opened Excel file
...
dynamic cellValue;
Worksheet sheet = null;
try
{
sheet = (Worksheet)m_Workbook.Worksheets[0];
cellValue = sheet.get_Range("E33").Value2;
}
catch (Exception e)
{
...
Data validation or comboboxes should not factor into this.
For information on Text vs Value vs Value2, see https://fastexcel.wordpress.com/2011/11/30/text-vs-value-vs-value2-slow-text-and-how-to-avoid-it/

How to set active sheet with Open XML SDK 2.5

using the example here How to Copy a Worksheet within a Workbook
I have successfully been able to clone/copy sheets in my excel file, however when I open the excel the 2nd sheet is the active(visible) sheet. I haven't been able to locate a property that could do thins.....Is there any way to specify what sheet is active?
I've tried to force it by opening and editing the first sheet in the file thinking it was the last edited sheet that was active but that didn't work either.
any help would be great. TIA
update: looking at the workbook.xml created when renaming the .xlsx to .zip I came accross the 'activeTab' property. made a quick change to my code and seems to work just fine
public void SetFirstSheetInFocus(String xlsxFile)
{
using (SpreadsheetDocument spreadSheet = SpreadsheetDocument.Open(xlsxFile, true))
{
//Get a reference to access the main Workbook part, which contains all references
WorkbookPart _workbookPart = spreadSheet.WorkbookPart;
if (_workbookPart != null)
{
WorkbookView _workbookView = spreadSheet.WorkbookPart.Workbook.BookViews.ChildElements.First<WorkbookView>();
if (_workbookView != null)
{
_workbookView.ActiveTab = 0; // 0 for first or whatever tab you want to use
}
// Save the workbook.
_workbookPart.Workbook.Save();
}
}
}
If the name of your sheet is in the variable
sheetName
you can set the sheet with that name active like this:
using (var spreadsheetDoc = SpreadsheetDocument.Open(emptyHIPTemplatePath, true /* isEditable */, new OpenSettings { AutoSave = false }))
{
var workbookPart = spreadsheetDoc.WorkbookPart;
var workBook = spreadsheetDoc.WorkbookPart.Workbook;
var sheet = workBook.Descendants<Sheet>().FirstOrDefault(s => s.Name == sheetName);
var sheetIndex = workBook.Descendants<Sheet>().ToList().IndexOf(sheet);
var workBookView = workBook.Descendants<WorkbookView>().First();
workBookView.ActiveTab = Convert.ToUInt32(sheetIndex);
...
workBook.Save();
}
From Vincent Tan's book:
The SheetId property doesn't determine the order. The order of
appending the Sheet classes to the Sheets class, does.
When you add a sheet, it gets the next index, but a single sheet does not have an index. OpenXML gives it an index when you are done adding sheets. Again, from Vincent Tan's book:
Let's say you have 3 worksheets named Sheet1, Sheet2 and Sheet3.
However, when you appended the corresponding Sheet classes, you did it
as Sheet2, Sheet3 and Sheet1, in that order.

Delete non-empty worksheets from excel workbook

I want to delete some worksheets from an Excel workbook. When my program is loaded, it reads the sheets in the workbook, lists them in a gridview where the user can select which sheets should be in the output file. When the user hits the save button, I delete worksheets based on the selection and save the workbook. All that works. EXCEPT for when there is actually content in the worksheet. This will delete empty worksheets, but not worksheets with content.
foreach (var item in _view.Sheets)
{
Exc.Worksheet ws = wb.Worksheets[item.Name];
if (!item.Include)
{
ws.Delete();
}
}
Any clues?
try to turn off alerts:
app.DisplayAlerts = false;
foreach (var item in _view.Sheets)
{
Exc.Worksheet ws = wb.Worksheets[item.Name];
if (!item.Include)
{
ws.Delete();
}
}
app.DisplayAlerts = true;

excel cell coloring

I am using c# to color particular cells of excel file.
i am using
Application excel = new Application();
Workbook wb = excel.Workbooks.Open(destPath);
Worksheet ws = wb.Worksheets[1];
ws.Cells[row, clmn].Interior.Color = XlRgbColor.rgbBlack;
to color cells..But this is not working..
It is giving an exception on the last line where i am coloring the cells
"Exception from HRESULT: 0x800A03EC"
I am unable to fix the exception
Can anyone help me out..
It might not work because the worksheet is protected.
You can check it the following way:
Application excel = new Application();
Workbook wb = excel.Workbooks.Open(destPath);
Worksheet ws = wb.Worksheets[1];
bool wasProtected = ws.ProtectContents;
if (wasProtected == true)
{
// unprotect the worksheet
}
else
{
ws.Cells[row, clmn].Interior.Color = XlRgbColor.rgbBlack;
}
...
if (wasProtected == true)
{
// protect back the worksheet
}
In my previous project I used the following snippet to Color Cells in Excel:
ws.Cells[row, clmn].Interior.Color = System.Drawing.ColorTranslator.ToWin32(Color.Black);
I tested the code that you have on a new Excel file and it works fine. However, I was able to reproduce the error when testing with a row value of 0. So maybe the problem is with the row and clmn values. Otherwise it must be something specific to your Excel file... maybe a protected cell or something along those lines.

Categories