using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using Microsoft.Office.Interop.Excel;
namespace OfficeUtility
{
/// <summary>
/// Worksheet related
/// </summary>
public class ExcelWorksheetUtil : IDisposable
{
/// <summary>
/// Get Worksheet accoring to the worksheet name
/// </summary>
/// <param name="name"></param>
/// <param name="workbook"></param>
/// <returns></returns>
public static Worksheet GetWorksheet(string name, Workbook workbook)
{
foreach (Worksheet ws in workbook.Worksheets)
{
if (ws.Name == name)
{
return ws;
}
}
return null;
}
/// <summary>
/// Get the last used row number
/// </summary>
/// <param name="worksheet">The worksheet which is working on</param>
/// <returns>The last uesed row number</returns>
public int GetLastUsedRow(Worksheet worksheet)
{
return worksheet.UsedRange.Row + worksheet.UsedRange.Rows.Count - 1;
}
/// <summary>
/// Get the last used column number
/// </summary>
/// <param name="worksheet">The worksheet which is working on</param>
/// <returns>The last used column number</returns>
public int GetLastUsedCol(Worksheet worksheet)
{
return worksheet.UsedRange.Column + worksheet.UsedRange.Columns.Count - 1;
}
/// <summary>
/// Print excel worksheet
/// </summary>
/// <param name="worksheet">The worksheet which will be printed</param>
/// <param name="printerName">The printer which will be used to print the worksheet</param>
/// <param name="pageOrientation">The page orientation</param>
public static void PrintWorksheet(Worksheet worksheet, string printerName, XlPageOrientation pageOrientation)
{
worksheet.Activate();
if (!string.IsNullOrEmpty(printerName))
{
worksheet.PageSetup.Orientation = pageOrientation;
worksheet.PrintOut(Missing.Value,
Missing.Value,
1,
Missing.Value,
printerName,
Missing.Value,
Missing.Value,
Missing.Value);
}
}
#region IDisposable Members
public void Dispose()
{
}
#endregion
}
}