You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
240 lines
6.1 KiB
C#
240 lines
6.1 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Web;
|
|
|
|
namespace Estsh.Core.Services
|
|
{
|
|
public class MeExcel
|
|
{/// <summary>
|
|
/// 打印 EXCEL 文件到默认打印机
|
|
/// </summary>
|
|
/// <param name="excelFileName">EXCEL 文件路径</param>
|
|
public static void Print(string excelFileName)
|
|
{
|
|
MyWorkBook WorkBook = new MyWorkBook(false);
|
|
WorkBook.Open(excelFileName);
|
|
WorkBook.Print();
|
|
WorkBook.Quit();
|
|
WorkBook = null;
|
|
GC.Collect();
|
|
}
|
|
|
|
/// <summary>
|
|
/// 打印 EXCEL 文件到指定打印机
|
|
/// </summary>
|
|
/// <param name="excelFileName">EXCEL 文件路径</param>
|
|
/// <param name="printerName">打印机名称</param>
|
|
public static void Print(string excelFileName, string printerName)
|
|
{
|
|
MyWorkBook WorkBook = new MyWorkBook(false);
|
|
WorkBook.Open(excelFileName);
|
|
WorkBook.Print(printerName);
|
|
WorkBook.Quit();
|
|
WorkBook = null;
|
|
GC.Collect();
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 工作簿类
|
|
/// </summary>
|
|
public class MyWorkBook
|
|
{
|
|
Excel.Application myExcel = null;
|
|
Excel.Workbook myWorkBook = null;
|
|
|
|
public MySheet Sheets = null;
|
|
|
|
public MyWorkBook()
|
|
{
|
|
myExcel = new Excel.Application();
|
|
myExcel.Visible = true;
|
|
}
|
|
|
|
public MyWorkBook(bool Visible)
|
|
{
|
|
myExcel = new Excel.Application();
|
|
myExcel.Visible = Visible;
|
|
}
|
|
|
|
public void CreateNewWorkBook()
|
|
{
|
|
myWorkBook = myExcel.Workbooks.Add(true);
|
|
Sheets = new MySheet(myWorkBook);
|
|
}
|
|
|
|
public void Open(string _ExcelFile)
|
|
{
|
|
object MissingValue = Type.Missing;
|
|
|
|
myWorkBook = myExcel.Workbooks.Open(_ExcelFile, MissingValue,
|
|
MissingValue, MissingValue, MissingValue,
|
|
MissingValue, MissingValue, MissingValue,
|
|
MissingValue, MissingValue, MissingValue,
|
|
MissingValue, MissingValue);
|
|
|
|
Sheets = new MySheet(myWorkBook);
|
|
}
|
|
|
|
public void Add(string _SheetName)
|
|
{
|
|
Excel.Worksheet ws = (Excel.Worksheet)myWorkBook.Sheets.Add(Type.Missing, Type.Missing, Type.Missing, Type.Missing);
|
|
ws.Name = _SheetName;
|
|
}
|
|
|
|
public void Delete(int _SheetIndex)
|
|
{
|
|
((Excel.Worksheet)myWorkBook.Sheets[_SheetIndex]).Delete();
|
|
}
|
|
|
|
public void SaveAs(string _ExcelFile)
|
|
{
|
|
myWorkBook.SaveCopyAs(_ExcelFile);
|
|
}
|
|
|
|
public void PrintPreview()
|
|
{
|
|
myWorkBook.PrintPreview(true);
|
|
}
|
|
|
|
public void PrintPreview(bool EnabelChanges)
|
|
{
|
|
myWorkBook.PrintPreview(EnabelChanges);
|
|
}
|
|
|
|
public void Print()
|
|
{
|
|
myWorkBook.PrintOut(Type.Missing, Type.Missing, 1, false, Type.Missing, Type.Missing, 1, Type.Missing);
|
|
}
|
|
|
|
public void Print(string printerName)
|
|
{
|
|
myWorkBook.PrintOut(Type.Missing, Type.Missing, 1, false, printerName, Type.Missing, 1, Type.Missing);
|
|
}
|
|
|
|
public void Quit()
|
|
{
|
|
myWorkBook.Close(false, Type.Missing, Type.Missing);
|
|
myExcel.Quit();
|
|
int generation = System.GC.GetGeneration(myExcel);
|
|
myWorkBook = null;
|
|
myExcel = null;
|
|
System.GC.Collect(generation);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 释放资源
|
|
/// </summary>
|
|
/// <param name="o"></param>
|
|
private void NAR(object o)
|
|
{
|
|
try
|
|
{
|
|
System.Runtime.InteropServices.Marshal.ReleaseComObject(o);
|
|
}
|
|
catch { }
|
|
finally
|
|
{
|
|
o = null;
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 工作表类
|
|
/// </summary>
|
|
public class MySheet
|
|
{
|
|
Excel.Workbook myWorkBook = null;
|
|
Excel.Worksheet myWorkSheet = null;
|
|
|
|
public MyRange Cells = null;
|
|
|
|
public MySheet(Excel.Workbook _myWorkBook)
|
|
{
|
|
myWorkBook = _myWorkBook;
|
|
}
|
|
|
|
public int Count
|
|
{
|
|
get
|
|
{
|
|
return myWorkBook.Sheets.Count;
|
|
}
|
|
}
|
|
|
|
public string Name { get; set; }
|
|
|
|
public MySheet this[int index]
|
|
{
|
|
get
|
|
{
|
|
if (index > 0 && index <= myWorkBook.Sheets.Count)
|
|
{
|
|
myWorkSheet = (Excel.Worksheet)myWorkBook.Sheets[index];
|
|
Cells = new MyRange(myWorkSheet);
|
|
this.Name = myWorkSheet.Name;
|
|
return this;
|
|
}
|
|
else
|
|
{
|
|
return null;
|
|
}
|
|
}
|
|
}
|
|
|
|
public void Activate()
|
|
{
|
|
myWorkSheet.Activate();
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 单元格选中区域类
|
|
/// </summary>
|
|
public class MyRange
|
|
{
|
|
Excel.Worksheet mySheet = null;
|
|
Excel.Range myRange = null;
|
|
|
|
public MyRange(Excel.Worksheet _mySheet)
|
|
{
|
|
mySheet = _mySheet;
|
|
}
|
|
|
|
public MyRange this[int row, int column]
|
|
{
|
|
get
|
|
{
|
|
if (row > 0 && column > 0)
|
|
{
|
|
myRange = (Excel.Range)mySheet.Cells[row, column];
|
|
return this;
|
|
}
|
|
else
|
|
{
|
|
return null;
|
|
}
|
|
}
|
|
}
|
|
|
|
public void Select()
|
|
{
|
|
myRange.Select();
|
|
}
|
|
|
|
public string Text
|
|
{
|
|
get { return myRange.Text.ToString(); }
|
|
set { myRange.Value2 = value; }
|
|
}
|
|
|
|
public int BackColorIndex
|
|
{
|
|
get { return int.Parse(myRange.Interior.ColorIndex.ToString()); }
|
|
set { myRange.Interior.ColorIndex = value; }
|
|
}
|
|
}
|
|
}
|