📄 excelhelper.cs
字号:
}
}
/// <summary>
/// 复制行(在指定WorkSheet指定行下面复制指定数量行)
/// </summary>
/// <param name="sheetIndex"></param>
/// <param name="rowIndex"></param>
/// <param name="count"></param>
public void CopyRows(int sheetIndex,int rowIndex,int count)
{
if(sheetIndex > this.WorkSheetCount)
{
this.KillExcelProcess();
throw new Exception("索引超出范围,WorkSheet索引不能大于WorkSheet数量!");
}
try
{
workSheet = (Excel.Worksheet)workBook.Worksheets[sheetIndex];
range1 = (Excel.Range)workSheet.Rows[rowIndex,this.missing];
for(int i=1;i<=count;i++)
{
range2 = (Excel.Range)workSheet.Rows[rowIndex + i,this.missing];
range1.Copy(range2);
}
}
catch(Exception e)
{
this.KillExcelProcess();
throw e;
}
}
/// <summary>
/// 删除行
/// </summary>
/// <param name="rowIndex"></param>
/// <param name="count"></param>
public void DeleteRows(int rowIndex,int count)
{
try
{
for(int n=1;n<=this.WorkSheetCount;n++)
{
workSheet = (Excel.Worksheet)workBook.Worksheets[n];
range = (Excel.Range)workSheet.Rows[rowIndex,this.missing];
for(int i=0;i<count;i++)
{
range.Delete(Excel.XlDirection.xlDown);
}
}
}
catch(Exception e)
{
this.KillExcelProcess();
throw e;
}
}
/// <summary>
/// 删除行
/// </summary>
/// <param name="sheetIndex"></param>
/// <param name="rowIndex"></param>
/// <param name="count"></param>
public void DeleteRows(int sheetIndex,int rowIndex,int count)
{
if(sheetIndex > this.WorkSheetCount)
{
this.KillExcelProcess();
throw new Exception("索引超出范围,WorkSheet索引不能大于WorkSheet数量!");
}
try
{
workSheet = (Excel.Worksheet)workBook.Worksheets[sheetIndex];
range = (Excel.Range)workSheet.Rows[rowIndex,this.missing];
for(int i=0;i<count;i++)
{
range.Delete(Excel.XlDirection.xlDown);
}
}
catch(Exception e)
{
this.KillExcelProcess();
throw e;
}
}
#endregion
#region Column Methods
/// <summary>
/// 插列(在指定列右边插入指定数量列)
/// </summary>
/// <param name="columnIndex"></param>
/// <param name="count"></param>
public void InsertColumns(int columnIndex,int count)
{
try
{
for(int n=1;n<=this.WorkSheetCount;n++)
{
workSheet = (Excel.Worksheet)workBook.Worksheets[n];
range = (Excel.Range)workSheet.Columns[this.missing,columnIndex];
for(int i=0;i<count;i++)
{
range.Insert(Excel.XlDirection.xlDown);
}
}
}
catch(Exception e)
{
this.KillExcelProcess();
throw e;
}
}
/// <summary>
/// 插列(在指定WorkSheet指定列右边插入指定数量列)
/// </summary>
/// <param name="sheetIndex"></param>
/// <param name="columnIndex"></param>
/// <param name="count"></param>
public void InsertColumns(int sheetIndex,int columnIndex,int count)
{
if(sheetIndex > this.WorkSheetCount)
{
this.KillExcelProcess();
throw new Exception("索引超出范围,WorkSheet索引不能大于WorkSheet数量!");
}
try
{
workSheet = (Excel.Worksheet)workBook.Worksheets[sheetIndex];
range = (Excel.Range)workSheet.Columns[this.missing,columnIndex];
for(int i=0;i<count;i++)
{
range.Insert(Excel.XlDirection.xlDown);
}
}
catch(Exception e)
{
this.KillExcelProcess();
throw e;
}
}
/// <summary>
/// 复制列(在指定列右边复制指定数量列)
/// </summary>
/// <param name="columnIndex"></param>
/// <param name="count"></param>
public void CopyColumns(int columnIndex,int count)
{
try
{
for(int n=1;n<=this.WorkSheetCount;n++)
{
workSheet = (Excel.Worksheet)workBook.Worksheets[n];
// range1 = (Excel.Range)workSheet.Columns[columnIndex,this.missing];
range1 = (Excel.Range)workSheet.get_Range(this.IntToLetter(columnIndex)+"1",this.IntToLetter(columnIndex)+"10000");
for(int i=1;i<=count;i++)
{
// range2 = (Excel.Range)workSheet.Columns[this.missing,columnIndex + i];
range2 = (Excel.Range)workSheet.get_Range(this.IntToLetter(columnIndex+i)+"1",this.IntToLetter(columnIndex+i)+"10000");
range1.Copy(range2);
}
}
}
catch(Exception e)
{
this.KillExcelProcess();
throw e;
}
}
/// <summary>
/// 复制列(在指定WorkSheet指定列右边复制指定数量列)
/// </summary>
/// <param name="sheetIndex"></param>
/// <param name="columnIndex"></param>
/// <param name="count"></param>
public void CopyColumns(int sheetIndex,int columnIndex,int count)
{
if(sheetIndex > this.WorkSheetCount)
{
this.KillExcelProcess();
throw new Exception("索引超出范围,WorkSheet索引不能大于WorkSheet数量!");
}
try
{
workSheet = (Excel.Worksheet)workBook.Worksheets[sheetIndex];
// range1 = (Excel.Range)workSheet.Columns[Type.Missing,columnIndex];
range1 = (Excel.Range)workSheet.get_Range(this.IntToLetter(columnIndex)+"1",this.IntToLetter(columnIndex)+"10000");
for(int i=1;i<=count;i++)
{
// range2 = (Excel.Range)workSheet.Columns[Type.Missing,columnIndex + i];
range2 = (Excel.Range)workSheet.get_Range(this.IntToLetter(columnIndex+i)+"1",this.IntToLetter(columnIndex+i)+"10000");
range1.Copy(range2);
}
}
catch(Exception e)
{
this.KillExcelProcess();
throw e;
}
}
/// <summary>
/// 删除列
/// </summary>
/// <param name="columnIndex"></param>
/// <param name="count"></param>
public void DeleteColumns(int columnIndex,int count)
{
try
{
for(int n=1;n<=this.WorkSheetCount;n++)
{
workSheet = (Excel.Worksheet)workBook.Worksheets[n];
range = (Excel.Range)workSheet.Columns[this.missing,columnIndex];
for(int i=0;i<count;i++)
{
range.Delete(Excel.XlDirection.xlDown);
}
}
}
catch(Exception e)
{
this.KillExcelProcess();
throw e;
}
}
/// <summary>
/// 删除列
/// </summary>
/// <param name="sheetIndex"></param>
/// <param name="columnIndex"></param>
/// <param name="count"></param>
public void DeleteColumns(int sheetIndex,int columnIndex,int count)
{
if(sheetIndex > this.WorkSheetCount)
{
this.KillExcelProcess();
throw new Exception("索引超出范围,WorkSheet索引不能大于WorkSheet数量!");
}
try
{
workSheet = (Excel.Worksheet)workBook.Worksheets[sheetIndex];
range = (Excel.Range)workSheet.Columns[this.missing,columnIndex];
for(int i=0;i<count;i++)
{
range.Delete(Excel.XlDirection.xlDown);
}
}
catch(Exception e)
{
this.KillExcelProcess();
throw e;
}
}
#endregion
#region Range Methods
/// <summary>
/// 将指定范围区域拷贝到目标区域
/// </summary>
/// <param name="sheetIndex">WorkSheet索引</param>
/// <param name="startCell">要拷贝区域的开始Cell位置(比如:A10)</param>
/// <param name="endCell">要拷贝区域的结束Cell位置(比如:F20)</param>
/// <param name="targetCell">目标区域的开始Cell位置(比如:H10)</param>
public void RangeCopy(int sheetIndex,string startCell,string endCell,string targetCell)
{
if(sheetIndex > this.WorkSheetCount)
{
this.KillExcelProcess();
throw new Exception("索引超出范围,WorkSheet索引不能大于WorkSheet数量!");
}
try
{
workSheet = (Excel.Worksheet)workBook.Worksheets.get_Item(sheetIndex);
range1 = workSheet.get_Range(startCell,endCell);
range2 = workSheet.get_Range(targetCell,this.missing);
range1.Copy(range2);
}
catch(Exception e)
{
this.KillExcelProcess();
throw e;
}
}
/// <summary>
/// 将指定范围区域拷贝到目标区域
/// </summary>
/// <param name="sheetName">WorkSheet名称</param>
/// <param name="startCell">要拷贝区域的开始Cell位置(比如:A10)</param>
/// <param name="endCell">要拷贝区域的结束Cell位置(比如:F20)</param>
/// <param name="targetCell">目标区域的开始Cell位置(比如:H10)</param>
public void RangeCopy(string sheetName,string startCell,string endCell,string targetCell)
{
try
{
Excel.Worksheet sheet = null;
for(int i=1;i<=this.WorkSheetCount;i++)
{
workSheet = (Excel.Worksheet)workBook.Sheets.get_Item(i);
if(workSheet.Name == sheetName)
{
sheet = workSheet;
}
}
if(sheet != null)
{
for(int i=sheetCount;i>=1;i--)
{
range1 = sheet.get_Range(startCell,endCell);
range2 = sheet.get_Range(targetCell,this.missing);
range1.Copy(range2);
}
}
else
{
this.KillExcelProcess();
throw new Exception("名称为\"" + sheetName + "\"的工作表不存在");
}
}
catch(Exception e)
{
this.KillExcelProcess();
throw e;
}
}
/// <summary>
/// 自动填充
/// </summary>
public void RangAutoFill()
{
Excel.Range rng = workSheet.get_Range("B4", Type.Missing);
rng.Value2 = "星期一 ";
rng.AutoFill(workSheet.get_Range("B4","B9"),
Excel.XlAutoFillType.xlFillWeekdays);
rng = workSheet.get_Range("C4", Type.Missing);
rng.Value = "一月";
rng.AutoFill(workSheet.get_Range("C4","C9"),
Excel.XlAutoFillType.xlFillMonths);
rng = workSheet.get_Range("D4",Type.Missing);
rng.Value2 = "1";
rng.AutoFill(workSheet.get_Range("D4","D9"),
Excel.XlAutoFillType.xlFillSeries);
rng = workSheet.get_Range("E4",Type.Missing);
rng.Value2 = "3";
rng = workSheet.get_Range("E5",Type.Missing);
rng.Value2 = "6";
rng = workSheet.get_Range("E4","E5");
rng.AutoFill(workSheet.get_Range("E4","E9"),
Excel.XlAutoFillType.xlFillSeries);
}
/// <summary>
/// 应用样式
/// </summary>
public void ApplyStyle()
{
object missingValue = Type.Missing;
Excel.Range rng = workSheet.get_Range("B3","L23");
Excel.Style style;
try
{
style = workBook.Styles["NewStyle"];
}
// Style doesn't exist yet.
catch
{
style = workBook.Styles.Add("NewStyle", missingValue);
style.Font.Name = "Verdana";
style.Font.Size = 12;
style.Font.Color = 255;
style.Interior.Color = (200 << 16) | (200 << 8) | 200;
style.Interior.Pattern = Excel.XlPattern.xlPatternSolid;
}
rng.Value2 = "'Style Test";
rng.Style = "NewStyle";
rng.Columns.AutoFit();
}
#endregion
#region ExcelHelper Kit
/// <summary>
/// 将Excel列的字母索引值转换成整数索引值
/// </summary>
/// <param name="letter"></param>
/// <returns></returns>
public int LetterToInt(string letter)
{
int n = 0;
if(letter.Trim().Length == 0)
throw new Exception("不接受空字符串!");
if(letter.Length >= 2)
{
char c1 = letter.ToCharArray(0,2)[0];
char c2 = letter.ToCharArray(0,2)[1];
if(!char.IsLetter(c1) || !char.IsLetter(c2))
{
throw new Exception("格式不正确,必须是字母!");
}
c1 = char.ToUpper(c1);
c2 = char.ToUpper(c2);
int i = Convert.ToInt32(c1) - 64;
int j = Convert.ToInt32(c2) - 64;
n = i * 26 + j;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -