C# COM组件操作Excel
2015-07-08 22:49
471 查看
一、使用微软COM组件读Excle
将Excle中的变量信息读入varInfoDic, 其定义如下:
Dictionary<int, List<VarInfo>> varInfoDic = new Dictionary<int, List<VarInfo>>();
VarInfo类的定义如下:
public class VarInfo
{
public string varType;
public string varName;
public int varByteOffset;
}
这个读Excle的函数,实现如下:
private void readVarInfoXLSX()
{
int canId = 0;
string strCanId;
string strVarByteOffset;
Microsoft.Office.Interop.Excel.Application excelApp = new Microsoft.Office.Interop.Excel.Application();
Workbook tempWorkbook = null;
Worksheet tempSheet = null;
Range tempRange = null;
try
{
if (varInfoDic.Count != 0)
{
varInfoDic.Clear();
}
if (canIdValidList.Count != 0)
{
canIdValidList.Clear();
}
// 打开Excel文件
tempWorkbook = excelApp.Workbooks.Open(varInfoFile, Missing.Value, Missing.Value, Missing.Value,
Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value,
Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);
excelApp.Visible = false; //false
// 获取第一个sheet
tempSheet = (Worksheet)tempWorkbook.Sheets[1];
tempSheet.Activate();
for (int row = varInfoValidStartRow; row <= tempSheet.UsedRange.Rows.Count; row++)
{
VarInfo tempVarInfo = new VarInfo();
tempRange = (Range)tempSheet.Cells[row, 1];
strCanId = tempRange.Text.ToString().Trim();
if (!string.IsNullOrWhiteSpace(strCanId))
{
canId = Convert.ToInt32(strCanId, 16);
tempRange = (Range)tempSheet.Cells[row, 2];
tempVarInfo.varType = tempRange.Text.ToString().Trim();
if (!canIdValidList.Contains(canId))
{
canIdValidList.Add(canId);
}
tempRange = (Range)tempSheet.Cells[row, 3];
tempVarInfo.varName = tempRange.Text.ToString().Trim();
tempRange = (Range)tempSheet.Cells[row, 4];
strVarByteOffset = tempRange.Text.ToString().Trim();
tempVarInfo.varByteOffset = Convert.ToInt32(strVarByteOffset);
if (varInfoDic.ContainsKey(canId))
{
List<VarInfo> tempList;
varInfoDic.TryGetValue(canId, out tempList);
tempList.Add(tempVarInfo);
}
else
{
List<VarInfo> tempVarInfoList = new List<VarInfo>();
tempVarInfoList.Add(tempVarInfo);
varInfoDic.Add(canId, tempVarInfoList);
}
}
else
{
//待加入容错处理
continue;
}
}
}
catch (Exception ex)
{
MessageBox.Show("Read variable information error! Please do it again!", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
Console.WriteLine("[readVarInfoXLSX] Function Error!");
}
finally
{
// 释放资源
tempSheet = null;
tempWorkbook.Close();
tempWorkbook = null;
excelApp.Workbooks.Close();
excelApp.Quit();
excelApp = null;
}
}
二、使用微软COM组件写Excle
将Excle中的变量信息读入varInfoDic, 其定义如下:
Dictionary<int, List<VarInfo>> varInfoDic = new Dictionary<int, List<VarInfo>>();
VarInfo类的定义如下:
public class VarInfo
{
public string varType;
public string varName;
public int varByteOffset;
}
这个读Excle的函数,实现如下:
private void readVarInfoXLSX()
{
int canId = 0;
string strCanId;
string strVarByteOffset;
Microsoft.Office.Interop.Excel.Application excelApp = new Microsoft.Office.Interop.Excel.Application();
Workbook tempWorkbook = null;
Worksheet tempSheet = null;
Range tempRange = null;
try
{
if (varInfoDic.Count != 0)
{
varInfoDic.Clear();
}
if (canIdValidList.Count != 0)
{
canIdValidList.Clear();
}
// 打开Excel文件
tempWorkbook = excelApp.Workbooks.Open(varInfoFile, Missing.Value, Missing.Value, Missing.Value,
Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value,
Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);
excelApp.Visible = false; //false
// 获取第一个sheet
tempSheet = (Worksheet)tempWorkbook.Sheets[1];
tempSheet.Activate();
for (int row = varInfoValidStartRow; row <= tempSheet.UsedRange.Rows.Count; row++)
{
VarInfo tempVarInfo = new VarInfo();
tempRange = (Range)tempSheet.Cells[row, 1];
strCanId = tempRange.Text.ToString().Trim();
if (!string.IsNullOrWhiteSpace(strCanId))
{
canId = Convert.ToInt32(strCanId, 16);
tempRange = (Range)tempSheet.Cells[row, 2];
tempVarInfo.varType = tempRange.Text.ToString().Trim();
if (!canIdValidList.Contains(canId))
{
canIdValidList.Add(canId);
}
tempRange = (Range)tempSheet.Cells[row, 3];
tempVarInfo.varName = tempRange.Text.ToString().Trim();
tempRange = (Range)tempSheet.Cells[row, 4];
strVarByteOffset = tempRange.Text.ToString().Trim();
tempVarInfo.varByteOffset = Convert.ToInt32(strVarByteOffset);
if (varInfoDic.ContainsKey(canId))
{
List<VarInfo> tempList;
varInfoDic.TryGetValue(canId, out tempList);
tempList.Add(tempVarInfo);
}
else
{
List<VarInfo> tempVarInfoList = new List<VarInfo>();
tempVarInfoList.Add(tempVarInfo);
varInfoDic.Add(canId, tempVarInfoList);
}
}
else
{
//待加入容错处理
continue;
}
}
}
catch (Exception ex)
{
MessageBox.Show("Read variable information error! Please do it again!", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
Console.WriteLine("[readVarInfoXLSX] Function Error!");
}
finally
{
// 释放资源
tempSheet = null;
tempWorkbook.Close();
tempWorkbook = null;
excelApp.Workbooks.Close();
excelApp.Quit();
excelApp = null;
}
}
二、使用微软COM组件写Excle
相关文章推荐
- 使用Python生成Excel格式的图片
- c#调用COM组件
- Excel 曝出 Power Query 安全漏洞,1.2 亿用户易受远程 DDE 攻击
- excel vba 限制工作表的滚动区域代码
- excel vba 高亮显示当前行代码
- C#实现把指定数据写入串口
- C#中抽象方法与虚拟方法的区别
- c#中虚函数的相关使用方法
- C#实现多线程的同步方法实例分析
- C#中尾递归的使用、优化及编译器优化
- C#通用邮件发送类分享
- C#中this的用法集锦
- C#.NET获取拨号连接的宽带连接方法
- C#实现AddRange为数组添加多个元素的方法
- C#中Equality和Identity浅析
- C#生成饼形图及添加文字说明实例代码
- C#判等对象是否相等的方法汇总
- C#简单的向量用法实例教程
- C#托管堆对象实例包含内容分析
- C#实现按照指定长度在数字前补0方法小结