http://blog.163.com/it_software/blog/static/1238582962010518113524723/
/// <summary>
/// 隐藏指定索引的工作表
/// </summary>
/// <param name="sheetIndex"></param>
public void HiddenWorkSheet(int sheetIndex)
{
if(sheetIndex > this.WorkSheetCount)
{
this.KillExcelProcess();
throw new Exception("索引超出范围,WorkSheet索引不能大于WorkSheet数量!");
}
try
{
Excel.Worksheet sheet = null;
sheet = (Excel.Worksheet)workBook.Sheets.get_Item(sheetIndex);
sheet.Visible = Excel.XlSheetVisibility.xlSheetHidden;
}
catch(Exception e)
{
this.KillExcelProcess();
throw e;
}
}
/// <summary>
/// 在指定名称的工作表后面拷贝指定个数的该工作表的副本,并重命名
/// </summary>
/// <param name="sheetName">工作表名称</param>
/// <param name="sheetCount">工作表个数</param>
public void CopyWorkSheets(string sheetName,int sheetCount)
{
try
{
Excel.Worksheet sheet = null;
int sheetIndex = 0;
for(int i=1;i<=this.WorkSheetCount;i++)
{
workSheet = (Excel.Worksheet)workBook.Sheets.get_Item(i);
if(workSheet.Name == sheetName)
{
sheet = workSheet;
sheetIndex = workSheet.Index;
}
}
if(sheet != null)
{
for(int i=sheetCount;i>=1;i--)
{
sheet.Copy(this.missing,sheet);
}
//重命名
for(int i=sheetIndex;i<=sheetIndex+sheetCount;i++)
{
workSheet = (Excel.Worksheet)workBook.Sheets.get_Item(i);
workSheet.Name = sheetName + "-" + Convert.ToString(i - sheetIndex + 1);
}
}
else
{
this.KillExcelProcess();
throw new Exception("名称为\"" + sheetName + "\"的工作表不存在");
}
}
catch(Exception e)
{
this.KillExcelProcess();
throw e;
}
}
/// <summary>
/// 将一个工作表拷贝到另一个工作表后面,并重命名
/// </summary>
/// <param name="srcSheetIndex">拷贝源工作表索引</param>
/// <param name="aimSheetIndex">参照位置工作表索引,新工作表拷贝在该工作表后面</param>
/// <param name="newSheetName"></param>
public void CopyWorkSheet(int srcSheetIndex,int aimSheetIndex,string newSheetName)
{
if(srcSheetIndex > this.WorkSheetCount || aimSheetIndex > this.WorkSheetCount)
{
this.KillExcelProcess();
throw new Exception("索引超出范围,WorkSheet索引不能大于WorkSheet数量!");
}
try
{
Excel.Worksheet srcSheet = (Excel.Worksheet)workBook.Sheets.get_Item(srcSheetIndex);
Excel.Worksheet aimSheet = (Excel.Worksheet)workBook.Sheets.get_Item(aimSheetIndex);
srcSheet.Copy(this.missing,aimSheet);
//重命名
workSheet = (Excel.Worksheet)aimSheet.Next; //获取新拷贝的工作表
workSheet.Name = newSheetName;
}
catch(Exception e)
{
this.KillExcelProcess();
throw e;
}
}
/// <summary>
/// 根据名称删除工作表
/// </summary>
/// <param name="sheetName"></param>
public void DeleteWorkSheet(string sheetName)
{
try
{
Excel.Worksheet sheet = null;
//找到名称位sheetName的工作表
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)
{
sheet.Delete();
}
else
{
this.KillExcelProcess();
throw new Exception("名称为\"" + sheetName + "\"的工作表不存在");
}
}
catch(Exception e)
{
this.KillExcelProcess();
throw e;
}
}
/// <summary>
/// 根据索引删除工作表
/// </summary>
/// <param name="sheetIndex"></param>
public void DeleteWorkSheet(int sheetIndex)
{
if(sheetIndex > this.WorkSheetCount)
{
this.KillExcelProcess();
throw new Exception("索引超出范围,WorkSheet索引不能大于WorkSheet数量!");
}
try
{
Excel.Worksheet sheet = null;
sheet = (Excel.Worksheet)workBook.Sheets.get_Item(sheetIndex);
sheet.Delete();
}
catch(Exception e)
{
this.KillExcelProcess();
throw e;
}
}
#endregion
#region TextBox Methods
/// <summary>
/// 向指定文本框写入数据,对每个WorkSheet操作
/// </summary>
/// <param name="textboxName">文本框名称</param>
/// <param name="text">要写入的文本</param>
public void SetTextBox(string textboxName,string text)
{
for(int i=1;i<=this.WorkSheetCount;i++)
{
workSheet = (Excel.Worksheet)workBook.Worksheets.get_Item(i);
try
{
textBox = (Excel.TextBox)workSheet.TextBoxes(textboxName);
textBox.Text = text;
}
catch
{
this.KillExcelProcess();
throw new Exception("不存在ID为\"" + textboxName + "\"的文本框!");
}
}
}
/// <summary>
/// 向指定文本框写入数据,对指定WorkSheet操作
/// </summary>
/// <param name="sheetIndex">工作表索引</param>
/// <param name="textboxName">文本框名称</param>
/// <param name="text">要写入的文本</param>
public void SetTextBox(int sheetIndex,string textboxName,string text)
{
workSheet = (Excel.Worksheet)workBook.Worksheets.get_Item(sheetIndex);
try
{
textBox = (Excel.TextBox)workSheet.TextBoxes(textboxName);
textBox.Text = text;
}
catch
{
this.KillExcelProcess();
throw new Exception("不存在ID为\"" + textboxName + "\"的文本框!");
}
}
/// <summary>
/// 向文本框写入数据,对每个WorkSheet操作
/// </summary>
/// <param name="ht">Hashtable的键值对保存文本框的ID和数据</param>
public void SetTextBoxes(Hashtable ht)
{
if(ht.Count == 0) return;
for(int i=1;i<=this.WorkSheetCount;i++)
{
workSheet = (Excel.Worksheet)workBook.Worksheets.get_Item(i);
foreach(DictionaryEntry dic in ht)
{
try
{
textBox = (Excel.TextBox)workSheet.TextBoxes(dic.Key);
textBox.Text = dic.Value.ToString();
}
catch
{
this.KillExcelProcess();
throw new Exception("不存在ID为\"" + dic.Key.ToString() + "\"的文本框!");
}
}
}
}
/// <summary>
/// 向文本框写入数据,对指定WorkSheet操作
/// </summary>
/// <param name="ht">Hashtable的键值对保存文本框的ID和数据</param>
public void SetTextBoxes(int sheetIndex,Hashtable ht)
{
if(ht.Count == 0) return;
if(sheetIndex > this.WorkSheetCount)
{
this.KillExcelProcess();
throw new Exception("索引超出范围,WorkSheet索引不能大于WorkSheet数量!");
}
workSheet = (Excel.Worksheet)workBook.Worksheets.get_Item(sheetIndex);
foreach(DictionaryEntry dic in ht)
{
try
{
textBox = (Excel.TextBox)workSheet.TextBoxes(dic.Key);
textBox.Text = dic.Value.ToString();
}
catch
{
this.KillExcelProcess();
throw new Exception("不存在ID为\"" + dic.Key.ToString() + "\"的文本框!");
}
}
}
#endregion
#region Cell Methods
/// <summary>
/// 向单元格写入数据,对当前WorkSheet操作
/// </summary>
/// <param name="rowIndex">行索引</param>
/// <param name="columnIndex">列索引</param>
/// <param name="text">要写入的文本值</param>
public void SetCells(int rowIndex,int columnIndex,string text)
{
try
{
workSheet.Cells[rowIndex,columnIndex] = text;
}
catch
{
this.KillExcelProcess();
throw new Exception("向单元格[" + rowIndex + "," + columnIndex + "]写数据出错!");
}
}
/// <summary>
/// 向单元格写入数据,对指定WorkSheet操作
/// </summary>
/// <param name="sheetIndex">工作表索引</param>
/// <param name="rowIndex">行索引</param>
/// <param name="columnIndex">列索引</param>
/// <param name="text">要写入的文本值</param>
public void SetCells(int sheetIndex,int rowIndex,int columnIndex,string text)
{
try
{
this.ChangeCurrentWorkSheet(sheetIndex); //改变当前工作表为指定工作表
workSheet.Cells[rowIndex,columnIndex] = text;
}
catch
{
this.KillExcelProcess();
throw new Exception("向单元格[" + rowIndex + "," + columnIndex + "]写数据出错!");
}
}
/// <summary>
/// 向单元格写入数据,对每个WorkSheet操作
/// </summary>
/// <param name="ht">Hashtable的键值对保存单元格的位置索引(行索引和列索引用“,”隔开)和数据</param>
public void SetCells(Hashtable ht)
{
int rowIndex;
int columnIndex;
string position;
if(ht.Count == 0) return;
for(int i=1;i<=this.WorkSheetCount;i++)
{
workSheet = (Excel.Worksheet)workBook.Worksheets.get_Item(i);
foreach(DictionaryEntry dic in ht)
{
try
{
position = dic.Key.ToString();
rowIndex = Convert.ToInt32(position.Split(',')[0]);
columnIndex = Convert.ToInt32(position.Split(',')[1]);
workSheet.Cells[rowIndex,columnIndex] = dic.Value;
}
catch
{
this.KillExcelProcess();
throw new Exception("向单元格[" + dic.Key + "]写数据出错!");
}
}
}
}
/// <summary>
/// 向单元格写入数据,对指定WorkSheet操作
/// </summary>
/// <param name="ht">Hashtable的键值对保存单元格的位置索引(行索引和列索引用“,”隔开)和数据</param>
public void SetCells(int sheetIndex,Hashtable ht)
{
int rowIndex;
int columnIndex;
string position;
if(sheetIndex > this.WorkSheetCount)
{
this.KillExcelProcess();
throw new Exception("索引超出范围,WorkSheet索引不能大于WorkSheet数量!");
}
if(ht.Count == 0) return;
workSheet = (Excel.Worksheet)workBook.Worksheets.get_Item(sheetIndex);
foreach(DictionaryEntry dic in ht)
{
try
{
position = dic.Key.ToString();
rowIndex = Convert.ToInt32(position.Split(',')[0]);
columnIndex = Convert.ToInt32(position.Split(',')[1]);
workSheet.Cells[rowIndex,columnIndex] = dic.Value;
}
catch
{
this.KillExcelProcess();
throw new Exception("向单元格[" + dic.Key + "]写数据出错!");
}
}
}