- using System;
- using System.Data;
- using System.Configuration;
- using System.Collections;
- using Microsoft.Office.Core;
- namespace CommonPrj
- {
- public class ExcelAccess
- {
- private Excel.Application m_objExcelApp;
- private Excel.Workbook m_objExcelWorkBook;
- private Excel.Worksheet m_objExcelWorkSheet;
- private Excel.Worksheet m_objTempWorkSheet;
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- public ExcelAccess()
- {
-
- m_objExcelApp = new Excel.Application();
- m_objExcelApp.DisplayAlerts = false;
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- public void OpenExcelFile(string p_strExcelFileName)
- {
-
- m_objExcelWorkBook = m_objExcelApp.Workbooks.Open( p_strExcelFileName, Type.Missing,
- true,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,
- Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,
- Type.Missing,Type.Missing);
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- public void OpenExcelFile(string p_strExcelFileName,bool p_ReadOnlyFlag)
- {
-
- m_objExcelWorkBook = m_objExcelApp.Workbooks.Open(p_strExcelFileName, Type.Missing,
- p_ReadOnlyFlag, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
- Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
- Type.Missing, Type.Missing, Type.Missing);
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- public void SelectSheet(string p_strSheetName)
- {
-
- m_objExcelWorkSheet = (Excel.Worksheet)m_objExcelWorkBook.Sheets[p_strSheetName];
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- public void SelectSheet()
- {
-
- m_objExcelWorkSheet = (Excel.Worksheet)m_objExcelWorkBook.ActiveSheet;
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- public void SelectSheet(int p_intSheetIndex)
- {
- m_objExcelWorkSheet =(Excel.Worksheet)m_objExcelWorkBook.Sheets[p_intSheetIndex];
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- public void SelectTempSheet(string p_strSheetName)
- {
- m_objTempWorkSheet = (Excel.Worksheet)m_objExcelWorkBook.Sheets[p_strSheetName];
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- public void CopyWorkSheet()
- {
- m_objExcelWorkSheet.Copy(Type.Missing,
- m_objExcelWorkBook.Sheets[SheetCount()]);
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- public int SheetCount()
- {
- return m_objExcelWorkBook.Sheets.Count;
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- public string SheetName()
- {
- Excel.Worksheet objWorkSheet = (Excel.Worksheet)m_objExcelWorkBook.ActiveSheet;
- return objWorkSheet.Name;
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- public void SetCellValue(int p_intRows, int p_intColumn, string p_strValue)
- {
-
- if (p_intRows = 65528)
- {
- m_objExcelWorkSheet.Cells[p_intRows, p_intColumn] = p_strValue;
- }
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- public string GetCellValue(int p_intRows, int p_intColumn)
- {
- if (p_intRows = 65536)
- {
- Excel.Range objRange = (Excel.Range)m_objExcelWorkSheet.Cells[p_intRows, p_intColumn];
-
- if (objRange.Text == null)
- {
- return string.Empty;
- }
- else
- {
- return objRange.Text.ToString();
- }
- }
- else
- {
- return string.Empty;
- }
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- public void SetPageArea(string p_strStartCell,string p_strEndCell)
- {
- m_objExcelWorkSheet.PageSetup.PrintArea =
- p_strStartCell + ":" + p_strEndCell;
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- public void SaveAs(string p_strName)
- {
-
- m_objExcelWorkBook.SaveAs(p_strName, Type.Missing, Type.Missing, Type.Missing,
- Type.Missing,Type.Missing,Excel.XlSaveAsAccessMode.xlExclusive, Type.Missing,
- Type.Missing,Type.Missing,Type.Missing,Type.Missing);
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- public void Save()
- {
- m_objExcelWorkBook.Save();
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- public int RowsCount()
- {
-
- return m_objExcelWorkSheet.UsedRange.Rows.Count;
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- public int ColumnCount()
- {
-
- return m_objExcelWorkSheet.UsedRange.Columns.Count;
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- private void CloseWorkBook()
- {
- if (m_objExcelWorkSheet != null)
- {
-
- System.Runtime.InteropServices.Marshal.ReleaseComObject(m_objExcelWorkSheet);
- m_objExcelWorkSheet = null;
- }
- if (m_objTempWorkSheet != null)
- {
-
- System.Runtime.InteropServices.Marshal.ReleaseComObject(m_objTempWorkSheet);
- m_objTempWorkSheet = null;
- }
- if (m_objExcelWorkBook != null)
- {
- m_objExcelWorkBook.Close(false, Type.Missing, Type.Missing);
-
- System.Runtime.InteropServices.Marshal.ReleaseComObject(m_objExcelWorkBook);
- m_objExcelWorkBook = null;
- }
- GC.Collect();
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- public void Close()
- {
-
- CloseWorkBook();
- GC.Collect();
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- private void ExitApp()
- {
- CloseWorkBook();
-
- m_objExcelApp.Quit();
- if (m_objExcelApp != null)
- {
-
- System.Runtime.InteropServices.Marshal.ReleaseComObject(m_objExcelApp);
- m_objExcelApp = null;
- }
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- public void ExitApplication()
- {
- ExitApp();
- GC.Collect();
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- public void ChangeFontColor(int p_intRow, int p_intColumn,int p_intColorIndex)
- {
- if (p_intRow = 65528)
- {
-
- Excel.Range objRange = (Excel.Range)m_objExcelWorkSheet.Cells[p_intRow, p_intColumn];
-
- objRange.Font.ColorIndex = p_intColorIndex;
- }
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- public void ChangeFontColor(int p_intStartRow,int p_intStartColumn,int p_intEndRow,
- int p_intEndColumn,int p_intColorIndex)
- {
- if (p_intStartRow = 65528 p_intEndRow = 65528)
- {
-
- Excel.Range objRange = m_objExcelWorkSheet.get_Range(m_objExcelWorkSheet.Cells
- [p_intStartRow, p_intStartColumn], m_objExcelWorkSheet.Cells
- [p_intEndRow, p_intEndColumn]);
-
- objRange.Font.ColorIndex = p_intColorIndex;
- }
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- public void SetAlign(int p_intRow, int p_intColumn,
- Excel.XlHAlign Halign,Excel.XlVAlign Valign)
- {
- if (p_intRow = 65528)
- {
-
- Excel.Range objRange = m_objExcelWorkSheet.get_Range
- (m_objExcelWorkSheet.Cells[p_intRow, p_intColumn],
- m_objExcelWorkSheet.Cells[p_intRow, p_intColumn]);
-
- objRange.HorizontalAlignment = Halign;
-
- objRange.VerticalAlignment = Valign;
- }
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- public Excel.Range GetRange(int p_intStartRow, int p_intStartColumn,
- int p_intEndRow, int p_intEndColumn)
- {
- if (p_intStartRow = 65528 p_intEndRow = 65528)
- {
- Excel.Range objRange = m_objExcelWorkSheet.get_Range
- (m_objExcelWorkSheet.Cells[p_intStartRow, p_intStartColumn],
- m_objExcelWorkSheet.Cells[p_intEndRow, p_intEndColumn]);
- return objRange;
- }
- else
- {
- return null;
- }
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- public void SetBackColor(int p_intRow, int p_intColumn,int p_intcolorIndex)
- {
- if (p_intRow = 65528)
- {
- Excel.Range objRange = m_objExcelWorkSheet.get_Range
- (m_objExcelWorkSheet.Cells[p_intRow, p_intColumn],
- m_objExcelWorkSheet.Cells[p_intRow, p_intColumn]);
- objRange.Interior.ColorIndex = p_intcolorIndex;
- }
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- public void SetBackColor(int p_intStartRow, int p_intStartColumn,
- int p_intEndRow,int p_intEndColumn,int p_intcolorIndex)
- {
- if (p_intStartRow = 65528 p_intEndRow = 65528)
- {
- Excel.Range objRange = m_objExcelWorkSheet.get_Range
- (m_objExcelWorkSheet.Cells[p_intStartRow, p_intStartColumn],
- m_objExcelWorkSheet.Cells[p_intEndRow, p_intEndColumn]);
- objRange.Interior.ColorIndex = p_intcolorIndex;
- }
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- public void CopyToClipboard(Excel.Range p_objFromRange)
- {
- p_objFromRange.Copy(Type.Missing);
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- public void PasteFromClipboard(Excel.Range p_objDestRange)
- {
-
- p_objDestRange.PasteSpecial(Excel.XlPasteType.xlPasteAll,
- Excel.XlPasteSpecialOperation.xlPasteSpecialOperationNone,
- Type.Missing, Type.Missing);
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- public void SetRowHeigh(int p_intRow,int p_intHeight)
- {
- int intExcelTotalColumn = ColumnCount();
-
- if (p_intRow = 65528)
- {
- Excel.Range objRange = (Excel.Range)m_objExcelWorkSheet.Cells
- [p_intRow, intExcelTotalColumn];
- objRange.RowHeight = p_intHeight;
- }
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- public void SetRowHeigh(int p_intStartRow,int p_intEndRow,int p_intHeight)
- {
- int intExcelTotalColumn = ColumnCount();
- if (p_intStartRow = 65528 p_intEndRow = 65528)
- {
-
- Excel.Range objRange = (Excel.Range)m_objExcelWorkSheet.get_Range(
- m_objExcelWorkSheet.Cells[p_intStartRow, intExcelTotalColumn],
- m_objExcelWorkSheet.Cells[p_intEndRow, intExcelTotalColumn]);
- objRange.RowHeight = p_intHeight;
- }
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- public void SetSheetName(string p_strOldName, string p_strNewName)
- {
- Excel.Worksheet objSheet = (Excel.Worksheet)m_objExcelWorkBook.Sheets[p_strOldName];
- objSheet.Name = p_strNewName;
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- public void HideSheet(string p_strSheetName)
- {
- Excel.Worksheet objSheet = (Excel.Worksheet)m_objExcelWorkBook.Sheets[p_strSheetName];
- objSheet.Visible = Excel.XlSheetVisibility.xlSheetHidden;
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- private int ShapeIndex(string p_strShapeName)
- {
- string temp = p_strShapeName.Substring(4);
- return int.Parse(temp);
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- public bool SheetExist(string p_strSheetName)
- {
- foreach (Excel.Worksheet objSheet in m_objExcelWorkBook.Worksheets)
- {
- if (objSheet.Name == p_strSheetName)
- {
- return true;
- }
- }
- return false;
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- public void DeleteShapes(ArrayList ShapesName)
- {
- foreach (string ShapeName in ShapesName)
- {
- m_objExcelWorkSheet.Shapes.Item(ShapeName).Delete();
- }
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- public void SetSolidBorder(int p_intStartRow, int p_intStartColumn,
- int p_intEndRow, int p_intEndColumn)
- {
- Excel.Range objRange = GetRange(p_intStartRow, p_intStartColumn,
- p_intEndRow, p_intEndColumn);
- objRange.Borders[Excel.XlBordersIndex.xlDiagonalDown].LineStyle =
- Excel.XlLineStyle.xlLineStyleNone;
- objRange.Borders[Excel.XlBordersIndex.xlDiagonalUp].LineStyle =
- Excel.XlLineStyle.xlLineStyleNone;
- objRange.Borders[Excel.XlBordersIndex.xlEdgeLeft].LineStyle =
- Excel.XlLineStyle.xlContinuous;
- objRange.Borders[Excel.XlBordersIndex.xlEdgeLeft].Weight =
- Excel.XlBorderWeight.xlMedium;
- objRange.Borders[Excel.XlBordersIndex.xlEdgeTop].LineStyle =
- Excel.XlLineStyle.xlContinuous;
- objRange.Borders[Excel.XlBordersIndex.xlEdgeTop].Weight =
- Excel.XlBorderWeight.xlMedium;
- objRange.Borders[Excel.XlBordersIndex.xlEdgeBottom].LineStyle =
- Excel.XlLineStyle.xlContinuous;
- objRange.Borders[Excel.XlBordersIndex.xlEdgeBottom].Weight =
- Excel.XlBorderWeight.xlMedium;
- objRange.Borders[Excel.XlBordersIndex.xlEdgeRight].LineStyle =
- Excel.XlLineStyle.xlContinuous;
- objRange.Borders[Excel.XlBordersIndex.xlEdgeRight].Weight =
- Excel.XlBorderWeight.xlMedium;
- objRange.Borders[Excel.XlBordersIndex.xlInsideHorizontal]
- .LineStyle = Excel.XlLineStyle.xlLineStyleNone;
- objRange.Borders[Excel.XlBordersIndex.xlInsideVertical]
- .LineStyle = Excel.XlLineStyle.xlLineStyleNone;
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- public void SetDotBorder(int p_intStartRow, int p_intStartColumn,
- int p_intEndRow, int p_intEndColumn)
- {
- Excel.Range objRange = GetRange(p_intStartRow, p_intStartColumn,
- p_intEndRow, p_intEndColumn);
- objRange.Borders[Excel.XlBordersIndex.xlDiagonalDown].LineStyle =
- Excel.XlLineStyle.xlLineStyleNone;
- objRange.Borders[Excel.XlBordersIndex.xlDiagonalUp].LineStyle =
- Excel.XlLineStyle.xlLineStyleNone;
- objRange.Borders[Excel.XlBordersIndex.xlEdgeLeft].LineStyle =
- Excel.XlLineStyle.xlDashDot;
- objRange.Borders[Excel.XlBordersIndex.xlEdgeLeft].Weight =
- Excel.XlBorderWeight.xlMedium;
- objRange.Borders[Excel.XlBordersIndex.xlEdgeTop].LineStyle =
- Excel.XlLineStyle.xlDashDot;
- objRange.Borders[Excel.XlBordersIndex.xlEdgeTop].Weight =
- Excel.XlBorderWeight.xlMedium;
- objRange.Borders[Excel.XlBordersIndex.xlEdgeBottom].LineStyle =
- Excel.XlLineStyle.xlDashDot;
- objRange.Borders[Excel.XlBordersIndex.xlEdgeBottom].Weight =
- Excel.XlBorderWeight.xlMedium;
- objRange.Borders[Excel.XlBordersIndex.xlEdgeRight].LineStyle =
- Excel.XlLineStyle.xlDashDot;
- objRange.Borders[Excel.XlBordersIndex.xlEdgeRight].Weight =
- Excel.XlBorderWeight.xlMedium;
- objRange.Borders[Excel.XlBordersIndex.xlInsideHorizontal]
- .LineStyle = Excel.XlLineStyle.xlLineStyleNone;
- objRange.Borders[Excel.XlBordersIndex.xlInsideVertical]
- .LineStyle = Excel.XlLineStyle.xlLineStyleNone;
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- public void DeleteWorkSheet(string p_strSheetName)
- {
- Excel.Worksheet objSheet = (Excel.Worksheet)m_objExcelWorkBook.Worksheets[p_strSheetName];
- objSheet.Delete();
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- public void ChangeShapeStyle(string p_strShapeName)
- {
- Excel.Shape objShape = m_objExcelWorkSheet.Shapes.Item(p_strShapeName);
- objShape.Line.DashStyle = MsoLineDashStyle.msoLineSquareDot;
- }
- public void ProtectSheet(string p_strSheetName)
- {
- Excel.Worksheet objSheet = (Excel.Worksheet)m_objExcelWorkBook.Sheets[p_strSheetName];
- objSheet.Protect(Type.Missing, false, Type.Missing, false, Type.Missing, Type.Missing,
- Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
- Type.Missing, Type.Missing, Type.Missing, Type.Missing);
- }
- public Excel.Font GetCellFont(int nRow, int nColumn)
- {
- Excel.Range objRange =(Excel.Range) m_objExcelWorkSheet.Cells[nRow, nColumn];
- return objRange.Font;
- }
- public Excel.Shapes GetShapes()
- {
- return m_objExcelWorkSheet.Shapes;
- }
- public int GetShapeCount()
- {
- return m_objExcelWorkSheet.Shapes.Count;
- }
- }
- }
/jishubfqr_2BIHyEokjordNyxbNALWXer8NcW8HfjqSasLbC98_3D4858163 8
简首页