-
-
-
- using System;
- using System.Data;
- using System.Reflection;
- using System.IO;
- using Microsoft.Office.Core;
- using System.Windows.Forms;
- using Excel = Microsoft.Office.Interop.Excel;
-
- namespace Wage.Common
- {
-
-
-
-
-
-
-
-
- public class ExcelLib
- {
-
- #region Variables
- private Excel.Application excelApplication = null;
- private Excel.Workbooks excelWorkBooks = null;
- private Excel.Workbook excelWorkBook = null;
- private Excel.Worksheet excelWorkSheet = null;
- private Excel.Range excelRange = null;
- private Excel.Range excelCopySourceRange = null;
- private int excelActiveWorkSheetIndex;
- private string excelOpenFileName = "";
- private string excelSaveFileName = "";
- #endregion
-
- #region Properties
- public int ActiveSheetIndex
- {
- get
- {
- return excelActiveWorkSheetIndex;
- }
- set
- {
- excelActiveWorkSheetIndex = value;
- }
- }
- public string OpenFileName
- {
- get
- {
- return excelOpenFileName;
- }
- set
- {
- excelOpenFileName = value;
- }
- }
- public string SaveFileName
- {
- get
- {
- return excelSaveFileName;
- }
- set
- {
- excelSaveFileName = value;
- }
- }
- #endregion
-
-
-
-
-
-
- public ExcelLib()
- {
- excelApplication = null;
- excelWorkBooks = null;
- excelWorkBook = null;
- excelWorkSheet = null;
- ActiveSheetIndex = 1;
- }
-
-
-
- public bool OpenExcelFile()
- {
- if (excelApplication != null) CloseExcelApplication();
-
-
- if (excelOpenFileName == "")
- {
- throw new Exception("请选择文件!");
- }
- if (!File.Exists(excelOpenFileName))
- {
-
- throw new Exception(excelOpenFileName + "该文件不存在!");
- }
- try
- {
- excelApplication = new Excel.ApplicationClass();
- excelWorkBooks = excelApplication.Workbooks;
- excelWorkBook = ((Excel.Workbook)excelWorkBooks.Open(excelOpenFileName, 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, Missing.Value));
- excelWorkSheet = (Excel.Worksheet)excelWorkBook.Worksheets[excelActiveWorkSheetIndex];
- excelApplication.Visible = false;
-
- return true;
- }
- catch (Exception e)
- {
- CloseExcelApplication();
- MessageBox.Show("(1)没有安装Excel 2003;(2)或没有安装Excel 2003 .NET 可编程性支持;/n详细信息:"
- +e.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
-
- return false;
- }
- }
-
-
-
-
-
-
-
-
-
- public string getOneCellValue(int CellRowID, int CellColumnID)
- {
- if (CellRowID <= 0)
- {
- throw new Exception("行索引超出范围!");
- }
- string sValue = "";
- try
- {
- sValue = ((Excel.Range)excelWorkSheet.Cells[CellRowID, CellColumnID]).Text.ToString();
- }
- catch (Exception e)
- {
- CloseExcelApplication();
- throw new Exception(e.Message);
- }
- return (sValue);
- }
-
-
-
-
-
-
- public string[] getCellsValue(string StartCell, string EndCell)
- {
- string[] sValue = null;
-
-
- excelRange = (Excel.Range)excelWorkSheet.get_Range(StartCell, EndCell);
- sValue = new string[excelRange.Count];
- int rowStartIndex = ((Excel.Range)excelWorkSheet.get_Range(StartCell, StartCell)).Row;
- int columnStartIndex = ((Excel.Range)excelWorkSheet.get_Range(StartCell, StartCell)).Column;
- int rowNum = excelRange.Rows.Count;
- int columnNum = excelRange.Columns.Count;
- int index = 0;
- for (int i = rowStartIndex; i < rowStartIndex + rowNum; i++)
- {
- for (int j = columnStartIndex; j < columnNum + columnStartIndex; j++)
- {
-
- sValue[index] = ((Excel.Range)excelWorkSheet.Cells[i, j]).Text.ToString();
- index++;
- }
- }
-
-
-
-
-
-
-
- return (sValue);
- }
-
-
-
-
- public DataTable getAllCellsValue()
- {
- int columnCount = getTotalColumnCount();
- int rowCount = getTotalRowCount();
- DataTable dt = new DataTable();
-
- for (int columnID = 1; columnID <= columnCount; columnID++)
- {
- dt.Columns.Add(((Excel.Range)excelWorkSheet.Cells[1, columnID]).Text.ToString());
- }
-
- for (int rowID = 2; rowID <= rowCount; rowID++)
- {
- DataRow dr = dt.NewRow();
- for (int columnID = 1; columnID <= columnCount; columnID++)
- {
- dr[columnID - 1] = ((Excel.Range)excelWorkSheet.Cells[rowID, columnID]).Text.ToString();
-
- }
- dt.Rows.Add(dr);
- }
- return (dt);
- }
- public int getTotalRowCount()
- {
- int rowsNumber = 0;
- try
- {
- while (true)
- {
- if (((Excel.Range)excelWorkSheet.Cells[rowsNumber + 1, 1]).Text.ToString().Trim() == "" &
- ((Excel.Range)excelWorkSheet.Cells[rowsNumber + 2, 1]).Text.ToString().Trim() == "" &
- ((Excel.Range)excelWorkSheet.Cells[rowsNumber + 3, 1]).Text.ToString().Trim() == "")
- break;
- rowsNumber++;
- }
- }
- catch
- {
- return -1;
- }
- return rowsNumber;
- }
-
-
-
-
- public int getTotalColumnCount()
- {
- int columnNumber = 0;
- try
- {
- while (true)
- {
- if (((Excel.Range)excelWorkSheet.Cells[1, columnNumber + 1]).Text.ToString().Trim() == "" &
- ((Excel.Range)excelWorkSheet.Cells[1, columnNumber + 2]).Text.ToString().Trim() == "" &
- ((Excel.Range)excelWorkSheet.Cells[1, columnNumber + 3]).Text.ToString().Trim() == "")
- break;
- columnNumber++;
- }
- }
- catch
- {
- return -1;
- }
- return columnNumber;
- }
-
-
-
-
-
-
-
- public void setOneCellValue(int CellRowID, int CellColumnID, string Value)
- {
- try
- {
- excelRange = (Excel.Range)excelWorkSheet.Cells[CellRowID, CellColumnID];
- excelRange.Value2 = Value;
-
-
- excelRange = null;
- }
- catch (Exception e)
- {
- CloseExcelApplication();
- throw new Exception(e.Message);
- }
- }
-
-
-
-
- public void SetActiveWorkSheet(int SheetIndex)
- {
- if (SheetIndex <= 0)
- {
- throw new Exception("索引超出范围!");
- }
- try
- {
- ActiveSheetIndex = SheetIndex;
- excelWorkSheet = (Excel.Worksheet)excelWorkBook.Worksheets[ActiveSheetIndex];
- }
- catch (Exception e)
- {
- CloseExcelApplication();
- throw new Exception(e.Message);
- }
- }
-
-
-
-
-
-
- public void setCellsValue(string StartCell, string EndCell, string Value)
- {
- try
- {
- excelRange = excelWorkSheet.get_Range(StartCell, EndCell);
- excelRange.Value2 = Value;
- excelRange = null;
- }
- catch (Exception e)
- {
- CloseExcelApplication();
- throw new Exception(e.Message);
- }
- }
-
-
-
- public void setOneLineValues(int LineID, int StartCellColumnID, int EndCellColumnID, string[] Values)
- {
-
-
-
-
-
-
- for (int i = StartCellColumnID; i <= EndCellColumnID; i++)
- {
- setOneCellValue(LineID, i, Values[i]);
- }
- }
- public void setCellsBorder(string startCell, string endCell)
- {
-
- excelRange = excelWorkSheet.get_Range(startCell, endCell);
- excelRange.Borders[Excel.XlBordersIndex.xlEdgeLeft].LineStyle = Excel.XlLineStyle.xlContinuous;
- excelRange.Borders[Excel.XlBordersIndex.xlEdgeTop].LineStyle = Excel.XlLineStyle.xlContinuous;
- excelRange.Borders[Excel.XlBordersIndex.xlEdgeRight].LineStyle = Excel.XlLineStyle.xlContinuous;
- excelRange.Borders[Excel.XlBordersIndex.xlEdgeBottom].LineStyle = Excel.XlLineStyle.xlContinuous;
- excelRange.Borders[Excel.XlBordersIndex.xlInsideVertical].LineStyle = Excel.XlLineStyle.xlContinuous;
-
- }
-
- public void setOneCellBorder(int CellRowID, int CellColumnID)
- {
-
- excelRange = (Excel.Range)excelWorkSheet.Cells[CellRowID, CellColumnID];
-
- excelRange.Borders[Excel.XlBordersIndex.xlEdgeLeft].LineStyle = Excel.XlLineStyle.xlContinuous;
- excelRange.Borders[Excel.XlBordersIndex.xlEdgeTop].LineStyle = Excel.XlLineStyle.xlContinuous;
- excelRange.Borders[Excel.XlBordersIndex.xlEdgeRight].LineStyle = Excel.XlLineStyle.xlContinuous;
- excelRange.Borders[Excel.XlBordersIndex.xlEdgeBottom].LineStyle = Excel.XlLineStyle.xlContinuous;
-
-
- }
-
-
-
- public void SetColumnWidth(string startCell, string endCell, int size)
- {
-
- excelRange = excelWorkSheet.get_Range(startCell, endCell);
- excelRange.ColumnWidth = size;
- }
-
- public void SetOneCellFont(int CellRowID, int CellColumnID, string fontName, int fontSize)
- {
- excelRange = (Excel.Range)excelWorkSheet.Cells[CellRowID, CellColumnID];
- excelRange.Font.Name = fontName;
- excelRange.Font.Size = fontSize;
- }
-
- public void SetOneCellHorizontalAlignment(int CellRowID, int CellColumnID, Excel.Constants alignment)
- {
- excelRange = (Excel.Range)excelWorkSheet.Cells[CellRowID, CellColumnID];
- excelRange.HorizontalAlignment = alignment;
-
- }
-
- public void SetOneCellColumnWidth(int CellRowID, int CellColumnID, int size)
- {
-
- excelRange = (Excel.Range)excelWorkSheet.Cells[CellRowID, CellColumnID];
- excelRange.ColumnWidth = size;
-
- }
-
-
-
-
-
-
-
- public void setOneCellNumberFormat(int CellRowID, int CellColumnID, string numberFormat)
- {
- try
- {
- excelRange = (Excel.Range)excelWorkSheet.Cells[CellRowID, CellColumnID];
- excelRange.NumberFormatLocal = numberFormat;
-
- excelRange = null;
- }
- catch (Exception e)
- {
- CloseExcelApplication();
- throw new Exception(e.Message);
- }
- }
- public void SetRowHeight(string startCell, string endCell, int size)
- {
-
- excelRange = excelWorkSheet.get_Range(startCell, endCell);
- excelRange.RowHeight = size;
-
- }
- public void SetRowHeight(int CellRowID, int CellColumnID, float size)
- {
-
- excelRange = (Excel.Range)excelWorkSheet.Cells[CellRowID, CellColumnID];
- excelRange.RowHeight = size;
-
- }
- public void SetOneCellRowHeight(int CellRowID, int CellColumnID, int size)
- {
-
- excelRange = (Excel.Range)excelWorkSheet.Cells[CellRowID, CellColumnID];
- excelRange.RowHeight = size;
-
- }
-
-
-
-
-
-
-
-
- public void CopyCells(string SourceStart, string SourceEnd, string DesStart, string DesEnd)
- {
- try
- {
- excelCopySourceRange = excelWorkSheet.get_Range(SourceStart, SourceEnd);
- excelRange = excelWorkSheet.get_Range(DesStart, DesEnd);
- excelCopySourceRange.Copy(excelRange);
-
- excelCopySourceRange = null;
- excelRange = null;
- }
- catch (Exception e)
- {
- CloseExcelApplication();
- throw new Exception(e.Message);
- }
- }
- public void CopyWorksheet(int SourceWorksheetIndex, int DesWorksheetIndex)
- {
- try
- {
-
-
- Excel.Worksheet sheetSource = (Excel.Worksheet)excelWorkBook.Worksheets[SourceWorksheetIndex];
- sheetSource.Select(Missing.Value);
- Excel.Worksheet sheetDest = (Excel.Worksheet)excelWorkBook.Worksheets[DesWorksheetIndex];
- sheetSource.Copy(Missing.Value, sheetDest);
- }
- catch (Exception e)
- {
- CloseExcelApplication();
- throw new Exception(e.Message);
- }
- }
-
-
-
-
-
-
- public void InsertRow(int CellRowID, int RowNum)
- {
- if (CellRowID <= 0)
- {
- throw new Exception("行索引超出范围!");
- }
- if (RowNum <= 0)
- {
- throw new Exception("插入行数无效!");
- }
- try
- {
- excelRange = (Excel.Range)excelWorkSheet.Rows[CellRowID, Missing.Value];
- for (int i = 0; i < RowNum; i++)
- {
- excelRange.Insert(Excel.XlDirection.xlDown, Missing.Value);
- }
- excelRange = null;
- }
- catch (Exception e)
- {
- CloseExcelApplication();
- throw new Exception(e.Message);
- }
- }
-
-
-
-
-
- public Excel.Range FindFirstRange(Excel.Range xlRange, string FindText)
- {
-
-
- Excel.Range firstFind = null;
- firstFind = xlRange.Find(FindText, Missing.Value, Excel.XlFindLookIn.xlValues, Excel.XlLookAt.xlPart, Excel.XlSearchOrder.xlByRows, Excel.XlSearchDirection.xlNext, false, Missing.Value, Missing.Value);
- return firstFind;
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
- public bool CellValueIsNull(int CellLineID, int CellColumnID)
- {
-
-
- if ((((Excel.Range)excelWorkSheet.Cells[CellLineID, CellColumnID]).Text.ToString().Trim() != ""))
- return false;
- return true;
- }
-
-
-
- public void newWorkbook(string excelTemplate, string fileName)
- {
-
-
- excelWorkBook = excelWorkBooks.Add(excelTemplate);
- SaveFileName = "";
- SaveExcel();
- }
- public void newWorksheet()
- {
- excelWorkBook.Worksheets.Add(Missing.Value, Missing.Value, 1, Missing.Value);
- }
- public void setWorksheetName(int sheetIndex, string worksheetName)
- {
-
- Excel._Worksheet sheet = (Excel._Worksheet)(excelWorkBook.Worksheets[(object)sheetIndex]);
- sheet.Name = worksheetName;
- }
-
- public void mergeOneLineCells(string startCell, string endCell)
- {
-
- excelRange = excelWorkSheet.get_Range(startCell, endCell);
-
- excelRange.MergeCells = true;
- }
-
- public void HorizontalAlignmentCells(string startCell, string endCell, Excel.Constants alignment)
- {
-
- excelRange = excelWorkSheet.get_Range(startCell, endCell);
- excelRange.HorizontalAlignment = alignment;
- }
-
- public void VerticalAlignmentCells(string startCell, string endCell, Excel.Constants alignment)
- {
-
- excelRange = excelWorkSheet.get_Range(startCell, endCell);
- excelRange.VerticalAlignment = alignment;
- }
-
-
-
-
- private string ConvertColumnIndexToChar(int columnIndex)
- {
- if (columnIndex < 1 || columnIndex > 256)
- {
- MessageBox.Show("columnIndex=" + columnIndex + ",超出了有效范围(1-256)");
- return "A";
- }
- if (columnIndex >= 1 && columnIndex <= 26)
- {
- return "AA";
- }
- if (columnIndex >= 27 && columnIndex <= 256)
- {
- return "AA";
- }
- return "A";
- }
-
- public void SaveExcel()
- {
- if (excelSaveFileName == "")
- {
- throw new Exception("未指定要保存的文件名");
- }
- try
- {
-
- excelWorkSheet.SaveAs(excelSaveFileName, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Excel.XlSaveAsAccessMode.xlNoChange, Missing.Value, Missing.Value, Missing.Value);
-
- }
- catch (Exception e)
- {
- CloseExcelApplication();
- throw new Exception(e.Message);
- }
- }
-
-
-
-
-
- public void SaveExcelAsXML()
- {
- if (excelSaveFileName == "")
- {
- throw new Exception("未指定要保存的文件名");
- }
- try
- {
-
- excelWorkSheet.SaveAs(excelSaveFileName, Microsoft.Office.Interop.Excel.XlFileFormat.xlXMLSpreadsheet, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Excel.XlSaveAsAccessMode.xlNoChange, Missing.Value, Missing.Value, Missing.Value);
-
- }
- catch (Exception e)
- {
- CloseExcelApplication();
- throw new Exception(e.Message);
- }
- }
-
-
-
-
-
- public void CloseExcelApplication()
- {
- try
- {
- excelWorkBooks = null;
- excelWorkBook = null;
- excelWorkSheet = null;
- excelRange = null;
- if (excelApplication != null)
- {
- excelApplication.Workbooks.Close();
-
- excelApplication.Quit();
- excelApplication = null;
-
-
- }
- }
- finally
- {
- GC.Collect();
- GC.WaitForPendingFinalizers();
- GC.Collect();
- GC.WaitForPendingFinalizers();
- }
- }
- private void ReleaseAllRef(Object obj)
- {
-
- try
- {
- while (System.Runtime.InteropServices.Marshal.ReleaseComObject(obj) > 1) ;
- }
- finally
- {
- obj = null;
- }
- }
-
- }
- }
|