/// <summary> /// 获得终止行 /// </summary> /// <returns></returns> public int EndRow() { object tempEnd = ExcelUtilityMethod.GetProperty(m_range, "End", new object[] { Microsoft.Office.Interop.Excel.XlDirection.xlDown }); return((int)ExcelUtilityMethod.GetProperty(tempEnd, "Row")); }
/// <summary> /// 自动调整 /// </summary> public void AutoFit() { object temp = ExcelUtilityMethod.GetProperty(m_range, "EntireColumn"); ExcelUtilityMethod.UseMethod(temp, "AutoFit", null); return; }
/// <summary> /// 为Chart设置数据源 /// </summary> /// <param name="input"></param> public void SetSourceData(Range input) { Microsoft.Office.Interop.Excel.XlRowCol useType = Microsoft.Office.Interop.Excel.XlRowCol.xlColumns; ExcelUtilityMethod.UseMethod(m_chart, "SetSourceData", new object[] { input.ThisRangeObject, useType }); }
/// <summary> /// 获取一个Cell /// </summary> /// <param name="input"></param> /// <returns></returns> public void Cell(int row, int column, dynamic input) { string rangStr = ExcelUtilityMethod.ConvertToTitle(column + 1) + (row + 1).ToString(); object tenmpRange = ExcelUtilityMethod.GetProperty(m_workSheet, "Range", new object[] { rangStr }); ExcelUtilityMethod.SetProperty(tenmpRange, "Value", new object[] { input }); }
/// <summary> /// 获取一个Cell /// </summary> /// <param name="row"></param> /// <param name="column"></param> /// <returns></returns> public dynamic Cell(int row, int column) { string rangStr = ExcelUtilityMethod.ConvertToTitle(column + 1) + (row + 1).ToString(); object tenmpRange = ExcelUtilityMethod.GetProperty(m_workSheet, "Range", new object[] { rangStr }); return(ExcelUtilityMethod.GetProperty(tenmpRange, "Value")); }
/// <summary> /// 在此工作表添加一个图片 /// </summary> /// <param name="strTempPath"></param> /// <param name="left"></param> /// <param name="top"></param> /// <param name="tempImageHeight"></param> /// <param name="tempImageWidth"></param> /// <returns></returns> public Shape AddPicture(string strTempPath, float left, float top, float tempImageHeight, float tempImageWidth) { object tempShapes = ExcelUtilityMethod.GetProperty(m_workSheet, "Shapes"); object temp = ExcelUtilityMethod.UseMethod(tempShapes, "AddPicture", new object[] { strTempPath, Microsoft.Office.Core.MsoTriState.msoFalse, Microsoft.Office.Core.MsoTriState.msoTrue, left, top, tempImageWidth, tempImageHeight }); return(new Shape(temp)); }
/// <summary> /// 获取一个Range /// </summary> /// <param name="input"></param> /// <returns></returns> public Range Range(int row, int column) { string rangStr = ExcelUtilityMethod.ConvertToTitle(column + 1) + (row + 1).ToString(); object tenmpRange = ExcelUtilityMethod.GetProperty(m_workSheet, "Range", new object[] { rangStr }); return(new Range(tenmpRange)); }
/// <summary> /// 将图片拷贝到剪切板 /// </summary> public void CopyPicture() { ExcelUtilityMethod.UseMethod(m_thisShape, "CopyPicture", new object[] { Microsoft.Office.Interop.Excel.XlPictureAppearance.xlScreen, Microsoft.Office.Interop.Excel.XlCopyPictureFormat.xlBitmap }); }
/// <summary> /// 获取一个Range /// </summary> /// <param name="input"></param> /// <returns></returns> public Range Range(int rowOne, int columnOne, int rowTwo, int columnTwo) { string rangStrOne = ExcelUtilityMethod.ConvertToTitle(columnOne + 1) + (rowOne + 1).ToString(); string rangStrTwo = ExcelUtilityMethod.ConvertToTitle(columnTwo + 1) + (rowTwo + 1).ToString(); string rangStr = rangStrOne + ":" + rangStrTwo; object tenmpRange = ExcelUtilityMethod.GetProperty(m_workSheet, "Range", new object[] { rangStr }); return(new Range(tenmpRange)); }
/// <summary> /// 在此工作表添加一个图表 /// </summary> /// <param name="left"></param> /// <param name="top"></param> /// <returns></returns> public Chart AddChart(double left = 0.0d, double top = 0.0d) { Microsoft.Office.Interop.Excel.XlChartType use_ChartType = Microsoft.Office.Interop.Excel.XlChartType.xlColumnClustered; object tempshapes = ExcelUtilityMethod.GetProperty(m_workSheet, "Shapes"); object tempShape = ExcelUtilityMethod.UseMethod(tempshapes, "AddChart", new object[] { use_ChartType, left, top }); object tempChart = ExcelUtilityMethod.GetProperty(tempShape, "Chart"); return(new Chart(tempChart)); }
/// <summary> /// 退出 /// </summary> public void Quit() { ExcelUtilityMethod.UseMethod(latApplication, "Quit", null); try { Process tempProcess = Process.GetProcessById(pid); tempProcess.Kill(); } catch { ; } }
/// <summary> /// 全名称 /// </summary> /// <returns></returns> public string FullName() { object returnValue = ExcelUtilityMethod.UseMethod(m_workBook, "FullName", null); if (returnValue is string) { return((string)returnValue); } else //返回有误时(不是字符串格式)返回空字符串 { return(string.Empty); } }
/// <summary> /// 添加Chart /// </summary> /// <returns></returns> public Chart AddChart() { Microsoft.Office.Interop.Excel.XlChartType use_ChartType = Microsoft.Office.Interop.Excel.XlChartType.xlColumnClustered; Microsoft.Office.Interop.Excel.XlChartLocation use_XlLocation = Microsoft.Office.Interop.Excel.XlChartLocation.xlLocationAutomatic; object chartsObject = ExcelUtilityMethod.GetProperty(m_workBook, "Charts"); object addedChartObject = ExcelUtilityMethod.UseMethod(chartsObject, "Add", new object[] { Type.Missing, Type.Missing, 1 }); //图表形式 ExcelUtilityMethod.SetProperty(addedChartObject, "ChartType", new object[] { use_ChartType }); //图表位置 ExcelUtilityMethod.UseMethod(addedChartObject, "Location", new object[] { use_XlLocation }); return(new Chart(addedChartObject)); }
/// <summary> /// 打开一个文档 /// </summary> /// <param name="path"></param> /// <returns></returns> public Workbook Open(string path) { object thisObject; try { thisObject = ExcelUtilityMethod.UseMethod(workbooks, "Open", new object[] { path, false, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value }); } catch (Exception) { thisObject = ExcelUtilityMethod.UseMethod(workbooks, "Open", new object[] { path, false }); } return(new Workbook(thisObject)); }
/// <summary> /// 应用程序 /// </summary> /// <exception cref="ArgumentException">没有安装Excel时抛出</exception> public Application() { try { latApplication = Activator.CreateInstance(Type.GetTypeFromProgID("Excel.Application")); //获取Excel App的句柄 hwnd = new IntPtr(this.Hwnd); //通过Windows API获取Excel进程ID GetWindowThreadProcessId(hwnd, out pid); } catch { throw new ArgumentException("没有安装Microsoft Office Excel。"); } //属性名 string[] propertyNames = new string[] { "DisplayAlerts", "AlertBeforeOverwriting" , "Visible", "Interactive", "UserControl", "AskToUpdateLinks", "AutoFormatAsYouTypeReplaceHyperlinks", "DisplayClipboardWindow" , "DisplayDocumentActionTaskPane", "DisplayDocumentInformationPanel" , "DisplayExcel4Menus", "EnableCheckFileExtensions", "EnableLargeOperationAlert" , "MergeInstances", "MouseAvailable", "ScreenUpdating" }; foreach (var propertyName in propertyNames) { try { ExcelUtilityMethod.SetProperty(latApplication, propertyName, new object[] { false }); } catch (Exception) // 当设置属性出现异常时 { continue; //跳过 } } }
/// <summary> /// 设置图例 /// </summary> public void ApplyDataLabels() { ExcelUtilityMethod.UseMethod(m_chart, "ApplyDataLabels", null); }
/// <summary> /// 关闭此文档 /// </summary> public void Close() { ExcelUtilityMethod.UseMethod(workbooks, "Close", null); }
/// <summary> /// 获得其中一个工作表 /// </summary> /// <param name="index"></param> /// <returns></returns> public Worksheet Get_Item(int index) { object thisObject = ExcelUtilityMethod.GetProperty(m_workSheets, "Item", new object[] { index }); return(new Worksheet(thisObject)); }
/// <summary> /// 获取一个Range /// </summary> /// <param name="input"></param> /// <returns></returns> public Range Range(string input) { return(new Range(ExcelUtilityMethod.GetProperty(m_workSheet, "Range", new object[] { input, Missing.Value }))); }
/// <summary> /// 批量输入数据 /// </summary> /// <param name="input"></param> public void SetValue2(object[,] input) { ExcelUtilityMethod.SetProperty(m_range, "Value2", new object[] { input }); }
/// <summary> /// 关闭工作簿 /// </summary> public void Close() { ExcelUtilityMethod.UseMethod(m_workBook, "Close", new object[] { null, null, null }); }
/// <summary> /// 保存 /// </summary> public void Save() { ExcelUtilityMethod.UseMethod(m_workBook, "Save", null); }
/// <summary> /// 选择此工作表 /// </summary> public void Select() { ExcelUtilityMethod.UseMethod(m_workSheet, "Select", null); return; }
/// <summary> /// 另存为 /// </summary> /// <param name="strpath"></param> public void SaveAs(string strpath) { ExcelUtilityMethod.UseMethod(m_workBook, "SaveAs", new object[] { strpath }); }
/// <summary> /// 新建一个文档 /// </summary> /// <param name="path"></param> /// <returns></returns> public Workbook Add() { object thisObject = ExcelUtilityMethod.UseMethod(workbooks, "Add", null); return(new Workbook(thisObject)); }