三十三个excel操作总结Word格式文档下载.docx
《三十三个excel操作总结Word格式文档下载.docx》由会员分享,可在线阅读,更多相关《三十三个excel操作总结Word格式文档下载.docx(53页珍藏版)》请在冰点文库上搜索。
![三十三个excel操作总结Word格式文档下载.docx](https://file1.bingdoc.com/fileroot1/2023-5/5/8ba3fec6-c772-4963-a8f4-e1317b303a5c/8ba3fec6-c772-4963-a8f4-e1317b303a5c1.gif)
21插列
22复制列
23IntToLetter将Excel列的整数索引值转换为字符索引值
24删除列
25将指定范围区域拷贝到目标区域
26自动填充
27删除行
28应用样式
29将Excel列的字母索引值转换成整数索引值(
30输出指定格式的文件(支持格式:
HTML,CSV,TEXT,EXCEL)
31保存,将Excel文件另存为指定格式
32计算WorkSheet数量
33结束Excel进程
将一个已有Excel工作簿作为模板,并指定输出路径
#region公共方法
#regionExcelHelper
///<
summary>
///构造函数,将一个已有Excel工作簿作为模板,并指定输出路径
/summary>
paramname="
templetFilePath"
>
Excel模板文件路径<
/param>
outputFilePath"
输出Excel文件路径<
publicExcelHelper(stringtempletFilePath,stringoutputFilePath)
{
if(templetFilePath==null)
thrownewException("
Excel模板文件路径不能为空!
"
);
if(outputFilePath==null)
输出Excel文件路径不能为空!
if(!
File.Exists(templetFilePath))
指定路径的Excel模板文件不存在!
this.templetFile=templetFilePath;
this.outputFile=outputFilePath;
//创建一个Application对象并使其可见
beforeTime=DateTime.Now;
app=newExcel.ApplicationClass();
app.Visible=true;
afterTime=DateTime.Now;
//打开模板文件,得到WorkBook对象
workBook=app.Workbooks.Open(templetFile,missing,missing,missing,missing,missing,
missing,missing,missing,missing,missing,missing,missing);
//得到WorkSheet对象
workSheet=(Excel.Worksheet)workBook.Sheets.get_Item
(1);
}
打开一个已有的工作簿(2008-04-2410:
17:
08)
///构造函数,打开一个已有的工作簿
fileName"
Excel文件名<
publicExcelHelper(stringfileName)
File.Exists(fileName))
指定路径的Excel文件不存在!
//打开一个WorkBook
workBook=app.Workbooks.Open(fileName,
Type.Missing,Type.Missing,Type.Missing,Type.Missing,
Type.Missing,Type.Missing,Type.Missing,Type.Missing);
新建一个工作簿
///构造函数,新建一个工作簿
publicExcelHelper()
//新建一个WorkBook
workBook=app.Workbooks.Add(Type.Missing);
#endregion
将DataTable数据写入Excel文件(自动分页)
#regionDataExportMethods
///将DataTable数据写入Excel文件(自动分页)
dt"
DataTable<
rows"
每个WorkSheet写入多少行数据<
top"
表格数据起始行索引<
left"
表格数据起始列索引<
publicvoidDataTableToExcel(DataTabledt,introws,inttop,intleft)
introwCount=dt.Rows.Count;
//DataTable行数
intcolCount=dt.Columns.Count;
//DataTable列数
sheetCount=this.GetSheetCount(rowCount,rows);
//WorkSheet个数
//
StringBuildersb;
//复制sheetCount-1个WorkSheet对象
for(inti=1;
i<
sheetCount;
i++)
workSheet=(Excel.Worksheet)workBook.Worksheets.get_Item(i);
workSheet.Copy(missing,workBook.Worksheets[i]);
=sheetCount;
intstartRow=(i-1)*rows;
//记录起始行索引
intendRow=i*rows;
//记录结束行索引
//若是最后一个WorkSheet,那么记录结束行索引为源DataTable行数
if(i==sheetCount)
endRow=rowCount;
//获取要写入数据的WorkSheet对象,并重命名
workSheet.Name=sheetPrefixName+"
-"
+i.ToString();
//将dt中的数据写入WorkSheet
for(intj=0;
j<
endRow-startRow;
j++)
for(intk=0;
k<
colCount;
k++)
workSheet.Cells[top+j,left+k]=dt.Rows[startRow+j][k].ToString();
//利用二维数组批量写入
introw=endRow-startRow;
string[,]ss=newstring[row,colCount];
row;
ss[j,k]=dt.Rows[startRow+j][k].ToString();
range=(Excel.Range)workSheet.Cells[top,left];
range=range.get_Resize(row,colCount);
range.Value=ss;
#region利用Windwo粘贴板批量拷贝数据(在Web下面行不通)
将DataTable数据写入Excel文件(不分页)
///将DataTable数据写入Excel文件(不分页)
publicvoidDataTableToExcel(DataTabledt,inttop,intleft)
string[,]arr=newstring[rowCount,colCount];
rowCount;
arr[j,k]=dt.Rows[j][k].ToString();
range=range.get_Resize(rowCount,colCount);
range.Value=arr;
数据写入Excel文件自动分页,并指定要合并的列索引
///将DataTable数据写入Excel文件(自动分页,并指定要合并的列索引)
mergeColumnIndex"
DataTable中要合并相同行的列索引,从0开始<
publicvoidDataTableToExcel(DataTabledt,introws,inttop,intleft,intmergeColumnIndex)
//源DataTable行数
//源DataTable列数
//合并相同行
this.MergeRows(workSheet,left+mergeColumnIndex,top,rows);
MergeRows将指定索引列的数据相同的行合并
///将指定索引列的数据相同的行合并,对指定WorkSheet操作
columnIndex"
要合并的列索引<
beginRowIndex"
合并开始行索引<
要合并的行数<
privatevoidMergeRows(Excel.Worksheetsheet,intcolumnIndex,intbeginRowIndex,introws)
intbeginIndex=beginRowIndex;
intcount=0;
stringtext1;
stringtext2;
if(sheet==null)
return;
for(intj=beginRowIndex;
beginRowIndex+rows;
range1=(Excel.Range)sheet.Cells[j,columnIndex];
range2=(Excel.Range)sheet.Cells[j+1,columnIndex];
text1=range1.Text.ToString();
text2=range2.Text.ToString();
if(text1==text2)
++count;
else
{
if(count>
0)
this.MergeCells(sheet,beginIndex,columnIndex,beginIndex+count,columnIndex,text1);
beginIndex=j+1;
//设置开始合并行索引
count=0;
//计数器清0
改变当前工作表,隐藏指定的工作表
///改变当前工作表
sheetIndex"
工作表索引<
publicvoidChangeCurrentWorkSheet(intsheetIndex)
//若指定工作表索引超出范围,则不改变当前工作表
if(sheetIndex<
1)
if(sheetIndex>
this.WorkSheetCount)
this.workSheet=(Excel.Worksheet)this.workBook.Sheets.get_Item(sheetIndex);
///隐藏指定名称的工作表
sheetName"
工作表名称<
publicvoidHiddenWorkSheet(stringsheetName)
try
Excel.Worksheetsheet=null;
=this.WorkSheetCount;
workSheet=(Excel.Worksheet)workBook.Sheets.get_Item(i);
if(workSheet.Name==sheetName)
sheet=workSheet;
if(sheet!
=null)
sheet.Visible=Excel.XlSheetVisibility.xlSheetHidden;
this.KillExcelProcess();
名称为\"
+sheetName+"
\"
的工作表不存在"
catch(Exceptione)
throwe;
///隐藏指定索引的工作表
<
publicvoidHiddenWorkSheet(intsheetIndex)
索引超出范围,WorkSheet索引不能大于WorkSheet数量!
sheet=(Excel.Worksheet)workBook.Sheets.get_Item(sheetIndex);
this.KillExcelPro