using System; using System.Data; using System.Configuration; using System.Web; namespace AAAAA { /// <summary> /// ExcelEdit /// </summary> public class ExcelEdit { public string mFileName; private Microsoft.Office.Interop.Excel.Application oAP; private Microsoft.Office.Interop.Excel.Workbooks oWBS; private Microsoft.Office.Interop.Excel.Workbook oWB; public ExcelEdit() { }
public void Create() { oAP = new Microsoft.Office.Interop.Excel.Application(); oWBS = oAP.Workbooks; oWB = oWBS.Add(true); }
public string GetCellValue(Microsoft.Office.Interop.Excel.Worksheet workSheet, int x, int y) { return workSheet.Cells[x,y].ToString();
} public string GetCellValue(string workSheetName, int x, int y) { return GetSheet(workSheetName).Cells[x,y].ToString(); }
public void SetCellValue(Microsoft.Office.Interop.Excel.Worksheet workSheet, int x, int y, object value) { workSheet.Cells[x,y] = value; } public void SetCellValue(string workSheet, int x, int y, object value) { GetSheet(workSheet).Cells[x,y] = value; }
public void SetCellProperty(Microsoft.Office.Interop.Excel.Worksheet workSheet, int Startx, int Starty, int Endx, int Endy, string name, int size, int color, bool bold, object hAlignment ) { workSheet.get_Range(workSheet.Cells[Startx, Starty], workSheet.Cells[Endx, Endy]).Font.Name = name; workSheet.get_Range(workSheet.Cells[Startx, Starty], workSheet.Cells[Endx, Endy]).Font.Bold = bold; workSheet.get_Range(workSheet.Cells[Startx, Starty], workSheet.Cells[Endx, Endy]).Font.Size = size; workSheet.get_Range(workSheet.Cells[Startx, Starty], workSheet.Cells[Endx, Endy]).Font.Color = color; workSheet.get_Range(workSheet.Cells[Startx, Starty], workSheet.Cells[Endx, Endy]).HorizontalAlignment = hAlignment; } public void SetCellProperty(string workSheetName, int Startx, int Starty, int Endx, int Endy, string name, int size, int color, bool bold, object hAlignment ) { Microsoft.Office.Interop.Excel.Worksheet workSheet = GetSheet(workSheetName); workSheet.get_Range(workSheet.Cells[Startx, Starty], workSheet.Cells[Endx, Endy]).Font.Name = name; workSheet.get_Range(workSheet.Cells[Startx, Starty], workSheet.Cells[Endx, Endy]).Font.Bold = bold; workSheet.get_Range(workSheet.Cells[Startx, Starty], workSheet.Cells[Endx, Endy]).Font.Size = size; workSheet.get_Range(workSheet.Cells[Startx, Starty], workSheet.Cells[Endx, Endy]).Font.Color = color; workSheet.get_Range(workSheet.Cells[Startx, Starty], workSheet.Cells[Endx, Endy]).HorizontalAlignment = hAlignment; }
public void MergeCells(Microsoft.Office.Interop.Excel.Worksheet workSheet, int x1, int y1, int x2, int y2) { workSheet.get_Range(workSheet.Cells[x1, y1], workSheet.Cells[x2, y2]).Merge(Type.Missing); } public void MergeCells(string workSheetName, int x1, int y1, int x2, int y2) { GetSheet(workSheetName).get_Range(GetSheet(workSheetName).Cells[x1, y1], GetSheet(workSheetName).Cells[x2, y2]).Merge(Type.Missing); }
public void InsertTable(System.Data.DataTable dt, string workSheetName, int startX, int startY) { for (int i = 0; i <= dt.Rows.Count - 1; i++) { for (int j = 0; j <= dt.Columns.Count - 1; j++) { GetSheet(workSheetName).Cells[startX + i, j + startY] = dt.Rows[i][j].ToString(); } } } public void InsertTable(System.Data.DataTable dt, Microsoft.Office.Interop.Excel.Worksheet workSheet, int startX, int startY) { for (int i = 0; i <= dt.Rows.Count - 1; i++) { for (int j = 0; j <= dt.Columns.Count - 1; j++) { workSheet.Cells[startX + i, j + startY] = dt.Rows[i][j].ToString(); } } }
public bool Save() { if (mFileName == string.Empty) { return false; } else { try { oWB.Save(); return true; } catch (Exception ex) { throw new ApplicationException(ex.Message); } } }
using System; using System.Data; using System.Configuration; using System.Web; namespace AAAAA { /// <summary> /// ExcelEdit /// </summary> public class ExcelEdit { public string mFileName; private Microsoft.Office.Interop.Excel.Application oAP; private Microsoft.Office.Interop.Excel.Workbooks oWBS; private Microsoft.Office.Interop.Excel.Workbook oWB; public ExcelEdit() { }
public void Create() { oAP = new Microsoft.Office.Interop.Excel.Application(); oWBS = oAP.Workbooks; oWB = oWBS.Add(true); }
public string GetCellValue(Microsoft.Office.Interop.Excel.Worksheet workSheet, int x, int y) { return workSheet.Cells[x,y].ToString();
} public string GetCellValue(string workSheetName, int x, int y) { return GetSheet(workSheetName).Cells[x,y].ToString(); }
public void SetCellValue(Microsoft.Office.Interop.Excel.Worksheet workSheet, int x, int y, object value) { workSheet.Cells[x,y] = value; } public void SetCellValue(string workSheet, int x, int y, object value) { GetSheet(workSheet).Cells[x,y] = value; }
public void SetCellProperty(Microsoft.Office.Interop.Excel.Worksheet workSheet, int Startx, int Starty, int Endx, int Endy, string name, int size, int color, bool bold, object hAlignment ) { workSheet.get_Range(workSheet.Cells[Startx, Starty], workSheet.Cells[Endx, Endy]).Font.Name = name; workSheet.get_Range(workSheet.Cells[Startx, Starty], workSheet.Cells[Endx, Endy]).Font.Bold = bold; workSheet.get_Range(workSheet.Cells[Startx, Starty], workSheet.Cells[Endx, Endy]).Font.Size = size; workSheet.get_Range(workSheet.Cells[Startx, Starty], workSheet.Cells[Endx, Endy]).Font.Color = color; workSheet.get_Range(workSheet.Cells[Startx, Starty], workSheet.Cells[Endx, Endy]).HorizontalAlignment = hAlignment; } public void SetCellProperty(string workSheetName, int Startx, int Starty, int Endx, int Endy, string name, int size, int color, bool bold, object hAlignment ) { Microsoft.Office.Interop.Excel.Worksheet workSheet = GetSheet(workSheetName); workSheet.get_Range(workSheet.Cells[Startx, Starty], workSheet.Cells[Endx, Endy]).Font.Name = name; workSheet.get_Range(workSheet.Cells[Startx, Starty], workSheet.Cells[Endx, Endy]).Font.Bold = bold; workSheet.get_Range(workSheet.Cells[Startx, Starty], workSheet.Cells[Endx, Endy]).Font.Size = size; workSheet.get_Range(workSheet.Cells[Startx, Starty], workSheet.Cells[Endx, Endy]).Font.Color = color; workSheet.get_Range(workSheet.Cells[Startx, Starty], workSheet.Cells[Endx, Endy]).HorizontalAlignment = hAlignment; }
public void MergeCells(Microsoft.Office.Interop.Excel.Worksheet workSheet, int x1, int y1, int x2, int y2) { workSheet.get_Range(workSheet.Cells[x1, y1], workSheet.Cells[x2, y2]).Merge(Type.Missing); } public void MergeCells(string workSheetName, int x1, int y1, int x2, int y2) { GetSheet(workSheetName).get_Range(GetSheet(workSheetName).Cells[x1, y1], GetSheet(workSheetName).Cells[x2, y2]).Merge(Type.Missing); }
public void InsertTable(System.Data.DataTable dt, string workSheetName, int startX, int startY) { for (int i = 0; i <= dt.Rows.Count - 1; i++) { for (int j = 0; j <= dt.Columns.Count - 1; j++) { GetSheet(workSheetName).Cells[startX + i, j + startY] = dt.Rows[i][j].ToString(); } } } public void InsertTable(System.Data.DataTable dt, Microsoft.Office.Interop.Excel.Worksheet workSheet, int startX, int startY) { for (int i = 0; i <= dt.Rows.Count - 1; i++) { for (int j = 0; j <= dt.Columns.Count - 1; j++) { workSheet.Cells[startX + i, j + startY] = dt.Rows[i][j].ToString(); } } }
public bool Save() { if (mFileName == string.Empty) { return false; } else { try { oWB.Save(); return true; } catch (Exception ex) { throw new ApplicationException(ex.Message); } } }
using System.Data;
using System.Configuration;
using System.Web; namespace AAAAA
{ /// <summary>
/// ExcelEdit
/// </summary>
public class ExcelEdit
{ public string mFileName;
private Microsoft.Office.Interop.Excel.Application oAP;
private Microsoft.Office.Interop.Excel.Workbooks oWBS;
private Microsoft.Office.Interop.Excel.Workbook oWB; public ExcelEdit()
{
}
public void Create()
{
oAP = new Microsoft.Office.Interop.Excel.Application();
oWBS = oAP.Workbooks;
oWB = oWBS.Add(true);
}
public void Open(string fileName)
{
oAP = new Microsoft.Office.Interop.Excel.Application();
oWBS = oAP.Workbooks;
oWB = oWBS.Open( fileName, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
mFileName = fileName;
}
public Microsoft.Office.Interop.Excel.Worksheet GetSheet(string sheetName)
{
Microsoft.Office.Interop.Excel.Worksheet workSheet = (Microsoft.Office.Interop.Excel.Worksheet)oWB.Worksheets[sheetName];
return workSheet;
}
public object AddSheet(string sheetName)
{
Microsoft.Office.Interop.Excel.Worksheet workSheet = (Microsoft.Office.Interop.Excel.Worksheet)oWB.Worksheets.Add(Type.Missing, Type.Missing, Type.Missing, Type.Missing);
workSheet.Name = sheetName;
return workSheet;
}
public void DelSheet(string sheetName)
{
((Microsoft.Office.Interop.Excel.Worksheet)oWB.Worksheets[sheetName]).Delete();
}
public object RenameSheet(string oldSheetName, string newSheetName)
{
Microsoft.Office.Interop.Excel.Worksheet workSheet = (Microsoft.Office.Interop.Excel.Worksheet)oWB.Worksheets[oldSheetName];
workSheet.Name = newSheetName;
return workSheet;
}
public object RenameSheet(Microsoft.Office.Interop.Excel.Worksheet sheet, string newSheetName)
{
sheet.Name = newSheetName;
return sheet;
}
public string GetCellValue(Microsoft.Office.Interop.Excel.Worksheet workSheet, int x, int y)
{
return workSheet.Cells[x,y].ToString();
}
public string GetCellValue(string workSheetName, int x, int y)
{
return GetSheet(workSheetName).Cells[x,y].ToString();
}
public void SetCellValue(Microsoft.Office.Interop.Excel.Worksheet workSheet, int x, int y, object value)
{
workSheet.Cells[x,y] = value;
}
public void SetCellValue(string workSheet, int x, int y, object value)
{
GetSheet(workSheet).Cells[x,y] = value;
}
public void SetCellProperty(Microsoft.Office.Interop.Excel.Worksheet workSheet, int Startx, int Starty, int Endx, int Endy, string name, int size, int color, bool bold, object hAlignment
)
{
workSheet.get_Range(workSheet.Cells[Startx, Starty], workSheet.Cells[Endx, Endy]).Font.Name = name;
workSheet.get_Range(workSheet.Cells[Startx, Starty], workSheet.Cells[Endx, Endy]).Font.Bold = bold;
workSheet.get_Range(workSheet.Cells[Startx, Starty], workSheet.Cells[Endx, Endy]).Font.Size = size;
workSheet.get_Range(workSheet.Cells[Startx, Starty], workSheet.Cells[Endx, Endy]).Font.Color = color;
workSheet.get_Range(workSheet.Cells[Startx, Starty], workSheet.Cells[Endx, Endy]).HorizontalAlignment = hAlignment;
}
public void SetCellProperty(string workSheetName, int Startx, int Starty, int Endx, int Endy, string name, int size, int color, bool bold, object hAlignment
)
{
Microsoft.Office.Interop.Excel.Worksheet workSheet = GetSheet(workSheetName);
workSheet.get_Range(workSheet.Cells[Startx, Starty], workSheet.Cells[Endx, Endy]).Font.Name = name;
workSheet.get_Range(workSheet.Cells[Startx, Starty], workSheet.Cells[Endx, Endy]).Font.Bold = bold;
workSheet.get_Range(workSheet.Cells[Startx, Starty], workSheet.Cells[Endx, Endy]).Font.Size = size;
workSheet.get_Range(workSheet.Cells[Startx, Starty], workSheet.Cells[Endx, Endy]).Font.Color = color;
workSheet.get_Range(workSheet.Cells[Startx, Starty], workSheet.Cells[Endx, Endy]).HorizontalAlignment = hAlignment;
}
public void MergeCells(Microsoft.Office.Interop.Excel.Worksheet workSheet, int x1, int y1, int x2, int y2)
{ workSheet.get_Range(workSheet.Cells[x1, y1], workSheet.Cells[x2, y2]).Merge(Type.Missing);
}
public void MergeCells(string workSheetName, int x1, int y1, int x2, int y2)
{
GetSheet(workSheetName).get_Range(GetSheet(workSheetName).Cells[x1, y1], GetSheet(workSheetName).Cells[x2, y2]).Merge(Type.Missing);
}
public void InsertTable(System.Data.DataTable dt, string workSheetName, int startX, int startY)
{
for (int i = 0; i <= dt.Rows.Count - 1; i++)
{
for (int j = 0; j <= dt.Columns.Count - 1; j++)
{
GetSheet(workSheetName).Cells[startX + i, j + startY] = dt.Rows[i][j].ToString();
}
}
}
public void InsertTable(System.Data.DataTable dt, Microsoft.Office.Interop.Excel.Worksheet workSheet, int startX, int startY)
{
for (int i = 0; i <= dt.Rows.Count - 1; i++)
{
for (int j = 0; j <= dt.Columns.Count - 1; j++)
{
workSheet.Cells[startX + i, j + startY] = dt.Rows[i][j].ToString();
}
}
}
public bool Save()
{
if (mFileName == string.Empty)
{
return false;
}
else
{
try
{
oWB.Save();
return true;
}
catch (Exception ex)
{
throw new ApplicationException(ex.Message);
}
}
}
public bool SaveAs(object fileName)
{
try
{
oWB.SaveAs(fileName, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing);
return true;
}
catch (Exception ex)
{
throw new ApplicationException(ex.Message);
}
}
public void Close()
{
oWB.Close(true, mFileName, Type.Missing);
oWBS.Close();
oAP.Quit();
oWB = null;
oWBS = null;
oAP = null;
GC.Collect();
}
}
}
using System.Data;
using System.Configuration;
using System.Web; namespace AAAAA
{ /// <summary>
/// ExcelEdit
/// </summary>
public class ExcelEdit
{ public string mFileName;
private Microsoft.Office.Interop.Excel.Application oAP;
private Microsoft.Office.Interop.Excel.Workbooks oWBS;
private Microsoft.Office.Interop.Excel.Workbook oWB; public ExcelEdit()
{
}
public void Create()
{
oAP = new Microsoft.Office.Interop.Excel.Application();
oWBS = oAP.Workbooks;
oWB = oWBS.Add(true);
}
public void Open(string fileName)
{
oAP = new Microsoft.Office.Interop.Excel.Application();
oWBS = oAP.Workbooks;
oWB = oWBS.Open( fileName, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
mFileName = fileName;
}
public Microsoft.Office.Interop.Excel.Worksheet GetSheet(string sheetName)
{
Microsoft.Office.Interop.Excel.Worksheet workSheet = (Microsoft.Office.Interop.Excel.Worksheet)oWB.Worksheets[sheetName];
return workSheet;
}
public object AddSheet(string sheetName)
{
Microsoft.Office.Interop.Excel.Worksheet workSheet = (Microsoft.Office.Interop.Excel.Worksheet)oWB.Worksheets.Add(Type.Missing, Type.Missing, Type.Missing, Type.Missing);
workSheet.Name = sheetName;
return workSheet;
}
public void DelSheet(string sheetName)
{
((Microsoft.Office.Interop.Excel.Worksheet)oWB.Worksheets[sheetName]).Delete();
}
public object RenameSheet(string oldSheetName, string newSheetName)
{
Microsoft.Office.Interop.Excel.Worksheet workSheet = (Microsoft.Office.Interop.Excel.Worksheet)oWB.Worksheets[oldSheetName];
workSheet.Name = newSheetName;
return workSheet;
}
public object RenameSheet(Microsoft.Office.Interop.Excel.Worksheet sheet, string newSheetName)
{
sheet.Name = newSheetName;
return sheet;
}
public string GetCellValue(Microsoft.Office.Interop.Excel.Worksheet workSheet, int x, int y)
{
return workSheet.Cells[x,y].ToString();
}
public string GetCellValue(string workSheetName, int x, int y)
{
return GetSheet(workSheetName).Cells[x,y].ToString();
}
public void SetCellValue(Microsoft.Office.Interop.Excel.Worksheet workSheet, int x, int y, object value)
{
workSheet.Cells[x,y] = value;
}
public void SetCellValue(string workSheet, int x, int y, object value)
{
GetSheet(workSheet).Cells[x,y] = value;
}
public void SetCellProperty(Microsoft.Office.Interop.Excel.Worksheet workSheet, int Startx, int Starty, int Endx, int Endy, string name, int size, int color, bool bold, object hAlignment
)
{
workSheet.get_Range(workSheet.Cells[Startx, Starty], workSheet.Cells[Endx, Endy]).Font.Name = name;
workSheet.get_Range(workSheet.Cells[Startx, Starty], workSheet.Cells[Endx, Endy]).Font.Bold = bold;
workSheet.get_Range(workSheet.Cells[Startx, Starty], workSheet.Cells[Endx, Endy]).Font.Size = size;
workSheet.get_Range(workSheet.Cells[Startx, Starty], workSheet.Cells[Endx, Endy]).Font.Color = color;
workSheet.get_Range(workSheet.Cells[Startx, Starty], workSheet.Cells[Endx, Endy]).HorizontalAlignment = hAlignment;
}
public void SetCellProperty(string workSheetName, int Startx, int Starty, int Endx, int Endy, string name, int size, int color, bool bold, object hAlignment
)
{
Microsoft.Office.Interop.Excel.Worksheet workSheet = GetSheet(workSheetName);
workSheet.get_Range(workSheet.Cells[Startx, Starty], workSheet.Cells[Endx, Endy]).Font.Name = name;
workSheet.get_Range(workSheet.Cells[Startx, Starty], workSheet.Cells[Endx, Endy]).Font.Bold = bold;
workSheet.get_Range(workSheet.Cells[Startx, Starty], workSheet.Cells[Endx, Endy]).Font.Size = size;
workSheet.get_Range(workSheet.Cells[Startx, Starty], workSheet.Cells[Endx, Endy]).Font.Color = color;
workSheet.get_Range(workSheet.Cells[Startx, Starty], workSheet.Cells[Endx, Endy]).HorizontalAlignment = hAlignment;
}
public void MergeCells(Microsoft.Office.Interop.Excel.Worksheet workSheet, int x1, int y1, int x2, int y2)
{ workSheet.get_Range(workSheet.Cells[x1, y1], workSheet.Cells[x2, y2]).Merge(Type.Missing);
}
public void MergeCells(string workSheetName, int x1, int y1, int x2, int y2)
{
GetSheet(workSheetName).get_Range(GetSheet(workSheetName).Cells[x1, y1], GetSheet(workSheetName).Cells[x2, y2]).Merge(Type.Missing);
}
public void InsertTable(System.Data.DataTable dt, string workSheetName, int startX, int startY)
{
for (int i = 0; i <= dt.Rows.Count - 1; i++)
{
for (int j = 0; j <= dt.Columns.Count - 1; j++)
{
GetSheet(workSheetName).Cells[startX + i, j + startY] = dt.Rows[i][j].ToString();
}
}
}
public void InsertTable(System.Data.DataTable dt, Microsoft.Office.Interop.Excel.Worksheet workSheet, int startX, int startY)
{
for (int i = 0; i <= dt.Rows.Count - 1; i++)
{
for (int j = 0; j <= dt.Columns.Count - 1; j++)
{
workSheet.Cells[startX + i, j + startY] = dt.Rows[i][j].ToString();
}
}
}
public bool Save()
{
if (mFileName == string.Empty)
{
return false;
}
else
{
try
{
oWB.Save();
return true;
}
catch (Exception ex)
{
throw new ApplicationException(ex.Message);
}
}
}
public bool SaveAs(object fileName)
{
try
{
oWB.SaveAs(fileName, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing);
return true;
}
catch (Exception ex)
{
throw new ApplicationException(ex.Message);
}
}
public void Close()
{
oWB.Close(true, mFileName, Type.Missing);
oWBS.Close();
oAP.Quit();
oWB = null;
oWBS = null;
oAP = null;
GC.Collect();
}
}
}
去CSDN下載區 找 C# EXCEL 流
有實例,它不受 EXCEL 版本影響!