C++读取Excel(基于NPOI Com组件 C# )不依赖office版
1.C# dll
using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml;
namespace ExcelAction
{
[Guid("139AD8F9-BE82-4DE2-90EC-E2C75416C0BF")]
public interface IExcelRead
{
void ReadExcel(string a);
}
[Guid("58C7A359-161A-4F5A-810D-F56E72DB39E7")]
public class ExcelReadImpl : IExcelRead
{
public virtual void ReadExcel(string a)
{
return;
}
}
[Guid("080531D6-EC99-446E-BBEA-69B3F8CC6B11")]
public class ExcelRead : ExcelReadImpl
{
public override void ReadExcel(string filePath)
{
try
{
StringBuilder content = new StringBuilder(2000);
#region 读取Excel
string sheetName = null;
int captionRow = 0;
DataTable dtTab = new DataTable(sheetName);
IWorkbook workbook;
using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read))
{
if (filePath.EndsWith(".xlsx"))
workbook = new XSSFWorkbook(fs);
else if (filePath.EndsWith(".xls"))
workbook = new HSSFWorkbook(fs);
else
return;
}
ISheet sheet;
if (!string.IsNullOrWhiteSpace(sheetName))
sheet = workbook.GetSheet(sheetName);
else
sheet = workbook.GetSheetAt(0);
for (int j = 0; j < (sheet.GetRow(captionRow).LastCellNum); j++)
{
var columnName = sheet.GetRow(captionRow).GetCell(j).ToString();
dtTab.Columns.Add(columnName);
}
System.Collections.IEnumerator rows = sheet.GetRowEnumerator();
for (int i = 0; i <= captionRow; i++)
//跳过第一行, 第一行为标题
rows.MoveNext();
while (rows.MoveNext())
{
IRow row = (IRow)rows.Current;
DataRow dr = dtTab.NewRow();
for (int i = 0; i < dtTab.Columns.Count; i++)
{
ICell cell = row.GetCell(i);
dr[i] = cell?.ToString();
}
dtTab.Rows.Add(dr);
}
workbook.Close();
#endregion
#region 写入txt
foreach (DataRow dr in dtTab.Rows)
{
string str0 = dr[0].ToString().Replace("\n", "");
string str1 = dr[1].ToString().Replace("\n", "");
string str2 = dr[2].ToString().Replace("\n", "");
string str3 = dr[3].ToString().Replace("\n", "");
string str4 = dr[4].ToString().Replace("\n", "");
string str5 = dr[5].ToString().Replace("\n", "");
string str6 = dr[6].ToString().Replace("\n", "");
string str7 = dr[7].ToString().Replace("\n", "");
if (str0 != "END")
{
content.Append(str0);
content.Append("\r\n");
content.Append(str1);
content.Append("\r\n");
content.Append(str2);
content.Append("\r\n");
content.Append(str3);
content.Append("\r\n");
content.Append(str4);
content.Append("\r\n");
content.Append(str5);
content.Append("\r\n");
content.Append(str6);
content.Append("\r\n");
content.Append(str7);
content.Append("\r\n");
}
string param = content.ToString();
string dirpath = Environment.GetEnvironmentVariable("Temp");
System.IO.File.WriteAllText(@dirpath + "\\PipeClearanceCheckConfigurationTable.txt", param, Encoding.Default);
}
#endregion
}
catch (Exception e)
{
MessageBox.Show("管路间隙检查配置表被打开!", "警告");
}
}
}
[Guid("94A6644F-E53C-4DCE-A62C-DD11587A6522")]
public class ExcelRead_Norm : ExcelReadImpl
{
public override void ReadExcel(string filePath)
{
try
{
StringBuilder content = new StringBuilder(2000);
#region 读取Excel
string sheetName = null;
int captionRow = 0;
DataTable dtTab = new DataTable(sheetName);
IWorkbook workbook;
using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read))
{
if (filePath.EndsWith(".xlsx"))
workbook = new XSSFWorkbook(fs);
else if (filePath.EndsWith(".xls"))
workbook = new HSSFWorkbook(fs);
else
return;
}
ISheet sheet;
if (!string.IsNullOrWhiteSpace(sheetName))
sheet = workbook.GetSheet(sheetName);
else
sheet = workbook.GetSheetAt(0);
for (int j = 0; j < (sheet.GetRow(captionRow).LastCellNum); j++)
{
var columnName = sheet.GetRow(captionRow).GetCell(j).ToString();
dtTab.Columns.Add(columnName);
}
System.Collections.IEnumerator rows = sheet.GetRowEnumerator();
for (int i = 0; i <= captionRow; i++)
//跳过第一行, 第一行为标题
rows.MoveNext();
while (rows.MoveNext())
{
IRow row = (IRow)rows.Current;
DataRow dr = dtTab.NewRow();
for (int i = 0; i < dtTab.Columns.Count; i++)
{
ICell cell = row.GetCell(i);
dr[i] = cell?.ToString();
}
dtTab.Rows.Add(dr);
}
workbook.Close();
#endregion
#region 写入txt
foreach (DataRow dr in dtTab.Rows)
{
string str0 = dr[0].ToString().Replace("\n", "");
string str1 = dr[1].ToString().Replace("\n", "");
string str2 = dr[2].ToString().Replace("\n", "");
string str3 = dr[3].ToString().Replace("\n", "");
if (str0 != "")
{
content.Append(str0);
content.Append("\r\n");
content.Append(str1);
content.Append("\r\n");
content.Append(str2);
content.Append("\r\n");
content.Append(str3);
content.Append("\r\n");
}
string param = content.ToString();
string dirpath = Environment.GetEnvironmentVariable("Temp");
System.IO.File.WriteAllText(@dirpath + "\\CommonPipeNameNamingSpecification.txt", param, Encoding.Default);
}
#endregion
}
catch (Exception e)
{
MessageBox.Show("管路公共名命名规范被打开!", "警告");
}
}
}
[Guid("3C0888A7-7887-433A-9441-656CC97E6F61")]
public class ExcelRead_Classify : ExcelReadImpl
{
public override void ReadExcel(string filePath)
{
try
{
StringBuilder content = new StringBuilder(2000);
#region 读取Excel
string sheetName = null;
int captionRow = 0;
DataTable dtTab = new DataTable(sheetName);
IWorkbook workbook;
using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read))
{
if (filePath.EndsWith(".xlsx"))
workbook = new XSSFWorkbook(fs);
else if (filePath.EndsWith(".xls"))
workbook = new HSSFWorkbook(fs);
else
return;
}
ISheet sheet;
int SheetCount = workbook.NumberOfSheets;
for (int k = 2; k < SheetCount; k++)
{
sheet = workbook.GetSheetAt(k);
for (int j = 0; j < (sheet.GetRow(captionRow).LastCellNum); j++)
{
var columnName = sheet.GetRow(captionRow).GetCell(j).ToString();
dtTab.Columns.Add(columnName);
}
System.Collections.IEnumerator rows = sheet.GetRowEnumerator();
for (int i = 0; i <= captionRow; i++)
//跳过第一行, 第一行为标题
rows.MoveNext();
while (rows.MoveNext())
{
IRow row = (IRow)rows.Current;
DataRow dr = dtTab.NewRow();
for (int i = 0; i < dtTab.Columns.Count; i++)
{
ICell cell = row.GetCell(i);
dr[i] = cell?.ToString();
}
dtTab.Rows.Add(dr);
}
#region 写入txt
content.Append(workbook.GetSheetName(k) + "<<<");
int num = 0;
foreach (DataRow dr in dtTab.Rows)
{
num++;
string str0 = dr[0].ToString().Replace("\n", "");
string str1 = dr[1].ToString().Replace("\n", "");
string str2 = dr[2].ToString().Replace("\n", "");
if (str0 != "")
{
content.Append(str0);
content.Append("\r\n");
content.Append(str1);
content.Append("\r\n");
if (num == dtTab.Rows.Count)
{
content.Append(str2 + "<<<over");
}
else
{
content.Append(str2);
}
content.Append("\r\n");
}
}
dtTab.Columns.Clear();
dtTab.Rows.Clear();
rows.Reset();
#endregion
}
#endregion
string param = content.ToString();
string dirpath = Environment.GetEnvironmentVariable("Temp");
System.IO.File.WriteAllText(@dirpath + "\\PipeSpaceClearanceCheckClassificationTable.txt", param, Encoding.Default);
workbook.Close();
}
catch (Exception e)
{
MessageBox.Show("管路空间间隙检查分类表被打开!", "警告");
}
}
}
[Guid("ED85BE10-A06E-4D68-82AE-53A0A9EB604F")]
public class Excel_Write_Data : ExcelReadImpl
{
[System.Runtime.ExceptionServices.HandleProcessCorruptedStateExceptions]
public override void ReadExcel(string filePath)
{
try
{
List<string> TotalList = new List<string>();
#region 读取txt
string dirpath = Path.GetDirectoryName(filePath);
StreamReader sr = new StreamReader(@dirpath + "\\家用分体外机产品结构检查表输出报告.txt", Encoding.Default);
string content = sr.ReadToEnd();
TotalList = content.Split(new string[] { "\r\n" }, StringSplitOptions.None).ToList();
#endregion
#region 写入Excel
XSSFWorkbook workbook = new XSSFWorkbook();
using (FileStream file = new FileStream(filePath, FileMode.Open, FileAccess.Read))
{
workbook = new XSSFWorkbook(file);
}
ICellStyle style = workbook.CreateCellStyle();
style.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Center;
ISheet sheet;
sheet = workbook.GetSheetAt(0);
//表格信息
IRow row;
ICell cell;
int row_num = 2;
for (int i = 0; i < TotalList.Count; i++)
{
int count = i % 8;
switch (count)
{
case 0:
row = sheet.GetRow(row_num);
cell = row.CreateCell(0);
cell.SetCellValue(TotalList[i]);
cell.CellStyle = style;
break;
case 1:
row = sheet.GetRow(row_num);
cell = row.CreateCell(1);
cell.SetCellValue(TotalList[i]);
cell.CellStyle = style;
break;
case 2:
row = sheet.GetRow(row_num);
cell = row.CreateCell(2);
cell.SetCellValue(TotalList[i]);
cell.CellStyle = style;
break;
case 3:
row = sheet.GetRow(row_num);
cell = row.CreateCell(3);
cell.SetCellValue(TotalList[i]);
cell.CellStyle = style;
break;
case 4:
row = sheet.GetRow(row_num);
cell = row.CreateCell(4);
cell.SetCellValue(TotalList[i]);
cell.CellStyle = style;
break;
case 5:
row = sheet.GetRow(row_num);
cell = row.CreateCell(5);
cell.SetCellValue(TotalList[i]);
cell.CellStyle = style;
break;
case 6:
row = sheet.GetRow(row_num);
cell = row.CreateCell(6);
cell.SetCellValue(TotalList[i]);
cell.CellStyle = style;
break;
case 7:
row = sheet.GetRow(row_num);
cell = row.CreateCell(7);
cell.SetCellValue(TotalList[i]);
cell.CellStyle = style;
row_num++;
break;
default:
break;
}
}
using (FileStream fs = new FileStream(filePath, FileMode.Create, FileAccess.Write))
{
workbook.Write(fs);
fs.Close();
}
#endregion
}
catch (Exception e)
{
MessageBox.Show("家用分体外机产品结构检查表输出报告:Excel写入失败!", "警告"); ;
}
}
}
[Guid("20BF41CD-DE01-48E1-A68B-0446CA169BDB")]
public class ExcelRead_Design : ExcelReadImpl
{
public override void ReadExcel(string filePath)
{
try
{
StringBuilder content = new StringBuilder(8000);
#region 读取Excel
string sheetName = null;
int captionRow = 0;
DataTable dtTab = new DataTable(sheetName);
IWorkbook workbook;
string dirpath = Environment.GetEnvironmentVariable("Temp");
try
{
using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read))
{
if (filePath.EndsWith(".xlsx"))
workbook = new XSSFWorkbook(fs);
else if (filePath.EndsWith(".xls"))
workbook = new HSSFWorkbook(fs);
else
return;
}
}
catch (Exception e)
{
byte[] Infos = File.ReadAllBytes(filePath);
byte[] keyArray = UTF8Encoding.UTF8.GetBytes("GELIJIA5H2C5S8D9");
byte[] ivArray = UTF8Encoding.UTF8.GetBytes("GELIJIA5S5F3G5S3");
RijndaelManaged rDel = new RijndaelManaged();
rDel.Padding = PaddingMode.None;
rDel.BlockSize = 128;
rDel.Key = keyArray;
rDel.IV = ivArray;
rDel.Mode = CipherMode.CBC;
ICryptoTransform cTransform = rDel.CreateDecryptor();
byte[] resultArray = cTransform.TransformFinalBlock(Infos, 0, Infos.Length);
filePath = dirpath + "\\PipeDesignParameterTable.xlsx";
using (FileStream fs = File.Create(filePath))
{
BufferedStream bs = new BufferedStream(fs);
bs.Write(resultArray, 0, Infos.Length);
}
using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read))
{
if (filePath.EndsWith(".xlsx"))
workbook = new XSSFWorkbook(fs);
else if (filePath.EndsWith(".xls"))
workbook = new HSSFWorkbook(fs);
else
return;
}
}
ISheet sheet;
if (!string.IsNullOrWhiteSpace(sheetName))
sheet = workbook.GetSheet(sheetName);
else
sheet = workbook.GetSheetAt(0);
for (int j = 0; j < (sheet.GetRow(captionRow).LastCellNum); j++)
{
var columnName = sheet.GetRow(captionRow).GetCell(j).ToString();
dtTab.Columns.Add(columnName);
}
System.Collections.IEnumerator rows = sheet.GetRowEnumerator();
for (int i = 0; i <= captionRow; i++)
//跳过第一行, 第一行为标题
rows.MoveNext();
while (rows.MoveNext())
{
IRow row = (IRow)rows.Current;
DataRow dr = dtTab.NewRow();
for (int i = 0; i < dtTab.Columns.Count; i++)
{
ICell cell = row.GetCell(i);
dr[i] = cell?.ToString();
}
dtTab.Rows.Add(dr);
}
workbook.Close();
#endregion
#region 写入txt
int count = 0;
List<string> title = new List<string>();
foreach (DataRow dr in dtTab.Rows)
{
if (count == 0)
{
for (int i = 1; i < 110; i++)
{
title.Add(dr[i].ToString().Replace("\n", ""));
}
}
else if (!String.IsNullOrEmpty(dr[0].ToString()))
{
for (int i = 1; i < 110; i++)
{
string str = dr[i].ToString().Replace("\n", "");
content.Append(title[i - 1] + "<<<" + str);
content.Append("\r\n");
}
}
else if (String.IsNullOrEmpty(dr[0].ToString()))
{
break;
}
count++;
}
string param = content.ToString();
System.IO.File.WriteAllText(@dirpath + "\\PipeDesignParameterTable.txt", param, Encoding.Default);
#endregion
}
catch (Exception e)
{
MessageBox.Show("管路设计参数表被打开!", "警告");
}
}
}
[Guid("8278610C-FB95-4B4A-97A6-9D00A9FB2F3E")]
public class ExcelRead_Check : ExcelReadImpl
{
public override void ReadExcel(string filePath)
{
try
{
StringBuilder content = new StringBuilder(4000);
#region 读取Excel
string sheetName = "标准模式";
int captionRow = 0;
DataTable dtTab = new DataTable(sheetName);
IWorkbook workbook;
using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read))
{
if (filePath.EndsWith(".xlsx"))
workbook = new XSSFWorkbook(fs);
else if (filePath.EndsWith(".xls"))
workbook = new HSSFWorkbook(fs);
else
return;
}
ISheet sheet;
if (!string.IsNullOrWhiteSpace(sheetName))
sheet = workbook.GetSheet(sheetName);
else
sheet = workbook.GetSheetAt(1);
for (int j = 0; j < (sheet.GetRow(captionRow).LastCellNum); j++)
{
var columnName = sheet.GetRow(captionRow).GetCell(j).ToString();
dtTab.Columns.Add(columnName);
}
System.Collections.IEnumerator rows = sheet.GetRowEnumerator();
for (int i = 0; i <= captionRow; i++)
//跳过第一行, 第一行为标题
rows.MoveNext();
while (rows.MoveNext())
{
IRow row = (IRow)rows.Current;
DataRow dr = dtTab.NewRow();
for (int i = 0; i < dtTab.Columns.Count; i++)
{
ICell cell = row.GetCell(i);
dr[i] = cell?.ToString();
}
dtTab.Rows.Add(dr);
}
workbook.Close();
#endregion
#region 写入txt
foreach (DataRow dr in dtTab.Rows)
{
string str0 = dr[0].ToString().Replace("\n", "");
string str1 = dr[1].ToString().Replace("\n", "");
string str2 = dr[2].ToString().Replace("\n", "");
string str3 = dr[3].ToString().Replace("\n", "");
string str4 = dr[4].ToString().Replace("\n", "");
string str5 = dr[5].ToString().Replace("\n", "");
if (str0 != "END")
{
content.Append(str0);
content.Append("\r\n");
content.Append(str1);
content.Append("\r\n");
content.Append(str2);
content.Append("\r\n");
content.Append(str3);
content.Append("\r\n");
content.Append(str4);
content.Append("\r\n");
content.Append(str5);
content.Append("\r\n");
}
if (str0 == "END")
{
string param = content.ToString();
string dirpath = Environment.GetEnvironmentVariable("Temp");
System.IO.File.WriteAllText(@dirpath + "\\StaticCheckSpecification.txt", param, Encoding.Default);
}
}
#endregion
}
catch (Exception e)
{
MessageBox.Show("静态检查规范表被打开!", "警告");
}
}
}
}
2.Com组件注册
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace ComRegister
{
class Program
{
static void Main(string[] args)
{
if (MessageBox.Show("请点击确定按钮,注册新的Com组件!", "注册Com组件", MessageBoxButtons.OKCancel) != DialogResult.OK)
{
return;
}
string FilePath = System.Environment.CurrentDirectory.Replace("\\","\\\\");
string Pattern = "[\u4e00-\u9fa5]";
MatchCollection Matches_cn = Regex.Matches(FilePath,Pattern,RegexOptions.IgnoreCase);
if (Matches_cn.Count != 0)
{
MessageBox.Show("文件目录包含中文,不能执行注册!", "警告");
return;
}
Process p = new Process();
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.CreateNoWindow = false;
p.Start();
p.StandardInput.WriteLine($"C:\\Windows\\Microsoft.NET\\Framework64\\v4.0.30319\\RegAsm.exe /u {FilePath}\\ExcelAction.dll /register /tlb:ExcelAction.tlb /codebase");
p.StandardInput.WriteLine($"C:\\Windows\\Microsoft.NET\\Framework64\\v4.0.30319\\RegAsm.exe {FilePath}\\ExcelAction.dll /register /tlb:ExcelAction.tlb /codebase");
p.StandardInput.WriteLine("pause");
p.StandardInput.WriteLine("&exit");
p.StandardInput.AutoFlush = true;
string strOut = p.StandardOutput.ReadToEnd();
p.WaitForExit();
p.Close();
MessageBox.Show("注册成功!", "提示");
}
}
}
3.C++调用
#pragma once
#include <vector>
#include <map>
#include <afxstr.h>
#include <comutil.h>
#include <boost\algorithm\string\classification.hpp>
#include <boost\algorithm\string\split.hpp>
#import "ExcelAction.tlb"
using std::vector;
using std::map;
extern void Textarea(CString str_ShowValue);
class ExcelReadOrWrite
{
public:
/*管路设计参数表*/
static int Excel_Read_Design(vector<vector<LixbXmlFatherNode>> &dataArray)
{
ProPath wtext_path;
CString strtext_path;
ProToolkitApplTextPathGet(wtext_path);
strtext_path = (CString)wtext_path;
#pragma region 调用com组件
CoInitialize(NULL);
CLSID clsidr;
ExcelAction::IExcelRead *ptr;
HRESULT hr = CLSIDFromProgID(OLESTR("ExcelAction.ExcelRead_Design"), &clsidr);
if (hr != S_OK)
{
MessageBoxShow(L"未能找到CLSID,检测注册表中名称是否正确.是否注册成功!");
return 0;
}
hr = CoCreateInstance(clsidr, NULL, CLSCTX_INPROC_SERVER, __uuidof(ExcelAction::IExcelRead), (LPVOID*)&ptr);
if (hr != S_OK)
{
MessageBoxShow(L"创建接口对象失败!");
return 0;
}
_bstr_t dirpath = (_bstr_t)strtext_path;
_bstr_t filepath = L"\\管路参数表\\管路设计参数表.xlsx";
CComBSTR strr;
strr.AppendBSTR(dirpath);
strr.AppendBSTR(filepath);
BSTR fullpath = strr.m_str;
ptr->ReadExcel(fullpath);
CoUninitialize();
#pragma endregion
#pragma region 读取txt
vector<string> labellist;
ifstream ifs;
CString str_Path;
str_Path.GetEnvironmentVariableW(L"Temp");
ifs.open(str_Path + L"\\PipeDesignParameterTable.txt");
string str;
while (getline(ifs, str))
{
labellist.push_back(str);
}
ifs.close();
LixbXmlFatherNode FatherNode;
LixbXmlChildrenNode ChildrenNode;
vector<LixbXmlFatherNode> datas;
vector<string> vec;
for (size_t i = 0; i < labellist.size(); i++)
{
int count = i % 109;
switch (count)
{
case 0:case 1:case 2:case 3:
boost::split(vec, labellist[i], boost::is_any_of("<<<"), boost::token_compress_on);
if (count != 3)
{
FatherNode.FatherName = L"基本属性";
ChildrenNode.ChildrenName = vec[0].c_str();
ChildrenNode.ChildrenValue = vec[1].c_str();
FatherNode.items.push_back(ChildrenNode);
}
else if (count == 3)
{
ChildrenNode.ChildrenName = vec[0].c_str();
ChildrenNode.ChildrenValue = vec[1].c_str();
FatherNode.items.push_back(ChildrenNode);
datas.push_back(FatherNode);
FatherNode.FatherName = L"";
FatherNode.items.clear();
}
break;
case 4:case 5:
boost::split(vec, labellist[i], boost::is_any_of("<<<"), boost::token_compress_on);
if (count != 5)
{
FatherNode.FatherName = L"折弯属性(自制)";
ChildrenNode.ChildrenName = vec[0].c_str();
ChildrenNode.ChildrenValue = vec[1].c_str();
FatherNode.items.push_back(ChildrenNode);
}
else if (count == 5)
{
ChildrenNode.ChildrenName = vec[0].c_str();
ChildrenNode.ChildrenValue = vec[1].c_str();
FatherNode.items.push_back(ChildrenNode);
datas.push_back(FatherNode);
FatherNode.FatherName = L"";
FatherNode.items.clear();
}
break;
case 6:case 7:case 8:
boost::split(vec, labellist[i], boost::is_any_of("<<<"), boost::token_compress_on);
if (count != 8)
{
FatherNode.FatherName = L"首段不扁(自制)";
ChildrenNode.ChildrenName = vec[0].c_str();
ChildrenNode.ChildrenValue = vec[1].c_str();
FatherNode.items.push_back(ChildrenNode);
}
else if (count == 8)
{
ChildrenNode.ChildrenName = vec[0].c_str();
ChildrenNode.ChildrenValue = vec[1].c_str();
FatherNode.items.push_back(ChildrenNode);
datas.push_back(FatherNode);
FatherNode.FatherName = L"";
FatherNode.items.clear();
}
break;
case 9:case 10:case 11:
boost::split(vec, labellist[i], boost::is_any_of("<<<"), boost::token_compress_on);
if (count != 11)
{
FatherNode.FatherName = L"尾段不扁(自制)";
ChildrenNode.ChildrenName = vec[0].c_str();
ChildrenNode.ChildrenValue = vec[1].c_str();
FatherNode.items.push_back(ChildrenNode);
}
else if (count == 11)
{
ChildrenNode.ChildrenName = vec[0].c_str();
ChildrenNode.ChildrenValue = vec[1].c_str();
FatherNode.items.push_back(ChildrenNode);
datas.push_back(FatherNode);
FatherNode.FatherName = L"";
FatherNode.items.clear();
}
break;
case 12:case 13:
boost::split(vec, labellist[i], boost::is_any_of("<<<"), boost::token_compress_on);
if (count != 13)
{
FatherNode.FatherName = L"折弯属性(采购)";
ChildrenNode.ChildrenName = vec[0].c_str();
ChildrenNode.ChildrenValue = vec[1].c_str();
FatherNode.items.push_back(ChildrenNode);
}
else if (count == 13)
{
ChildrenNode.ChildrenName = vec[0].c_str();
ChildrenNode.ChildrenValue = vec[1].c_str();
FatherNode.items.push_back(ChildrenNode);
datas.push_back(FatherNode);
FatherNode.FatherName = L"";
FatherNode.items.clear();
}
break;
case 14:case 15:case 16:
boost::split(vec, labellist[i], boost::is_any_of("<<<"), boost::token_compress_on);
if (count != 16)
{
FatherNode.FatherName = L"首段不扁(采购)";
ChildrenNode.ChildrenName = vec[0].c_str();
ChildrenNode.ChildrenValue = vec[1].c_str();
FatherNode.items.push_back(ChildrenNode);
}
else if (count == 16)
{
ChildrenNode.ChildrenName = vec[0].c_str();
ChildrenNode.ChildrenValue = vec[1].c_str();
FatherNode.items.push_back(ChildrenNode);
datas.push_back(FatherNode);
FatherNode.FatherName = L"";
FatherNode.items.clear();
}
break;
case 17:case 18:case 19:
boost::split(vec, labellist[i], boost::is_any_of("<<<"), boost::token_compress_on);
if (count != 19)
{
FatherNode.FatherName = L"尾段不扁(采购)";
ChildrenNode.ChildrenName = vec[0].c_str();
ChildrenNode.ChildrenValue = vec[1].c_str();
FatherNode.items.push_back(ChildrenNode);
}
else if (count == 19)
{
ChildrenNode.ChildrenName = vec[0].c_str();
ChildrenNode.ChildrenValue = vec[1].c_str();
FatherNode.items.push_back(ChildrenNode);
datas.push_back(FatherNode);
FatherNode.FatherName = L"";
FatherNode.items.clear();
}
break;
case 20:case 21:case 22:
boost::split(vec, labellist[i], boost::is_any_of("<<<"), boost::token_compress_on);
if (count != 22)
{
FatherNode.FatherName = L"夹管长度";
ChildrenNode.ChildrenName = vec[0].c_str();
ChildrenNode.ChildrenValue = vec[1].c_str();
FatherNode.items.push_back(ChildrenNode);
}
else if (count == 22)
{
ChildrenNode.ChildrenName = vec[0].c_str();
ChildrenNode.ChildrenValue = vec[1].c_str();
FatherNode.items.push_back(ChildrenNode);
datas.push_back(FatherNode);
FatherNode.FatherName = L"";
FatherNode.items.clear();
}
break;
case 23:
boost::split(vec, labellist[i], boost::is_any_of("<<<"), boost::token_compress_on);
FatherNode.FatherName = L"展开长度";
ChildrenNode.ChildrenName = vec[0].c_str();
ChildrenNode.ChildrenValue = vec[1].c_str();
FatherNode.items.push_back(ChildrenNode);
datas.push_back(FatherNode);
FatherNode.FatherName = L"";
FatherNode.items.clear();
break;
case 24:
boost::split(vec, labellist[i], boost::is_any_of("<<<"), boost::token_compress_on);
FatherNode.FatherName = L"直管";
ChildrenNode.ChildrenName = vec[0].c_str();
ChildrenNode.ChildrenValue = vec[1].c_str();
FatherNode.items.push_back(ChildrenNode);
datas.push_back(FatherNode);
FatherNode.FatherName = L"";
FatherNode.items.clear();
break;
case 25:case 26:case 27:
boost::split(vec, labellist[i], boost::is_any_of("<<<"), boost::token_compress_on);
if (count != 27)
{
FatherNode.FatherName = L"I型管";
ChildrenNode.ChildrenName = vec[0].c_str();
ChildrenNode.ChildrenValue = vec[1].c_str();
FatherNode.items.push_back(ChildrenNode);
}
else if (count == 27)
{
ChildrenNode.ChildrenName = vec[0].c_str();
ChildrenNode.ChildrenValue = vec[1].c_str();
FatherNode.items.push_back(ChildrenNode);
datas.push_back(FatherNode);
FatherNode.FatherName = L"";
FatherNode.items.clear();
}
break;
case 28:case 29:case 30:case 31:case 32:case 33:
boost::split(vec, labellist[i], boost::is_any_of("<<<"), boost::token_compress_on);
if (count != 33)
{
FatherNode.FatherName = L"扩喇叭口(管端成型)";
ChildrenNode.ChildrenName = vec[0].c_str();
ChildrenNode.ChildrenValue = vec[1].c_str();
FatherNode.items.push_back(ChildrenNode);
}
else if (count == 33)
{
ChildrenNode.ChildrenName = vec[0].c_str();
ChildrenNode.ChildrenValue = vec[1].c_str();
FatherNode.items.push_back(ChildrenNode);
datas.push_back(FatherNode);
FatherNode.FatherName = L"";
FatherNode.items.clear();
}
break;
case 34:case 35:case 36:case 37:
boost::split(vec, labellist[i], boost::is_any_of("<<<"), boost::token_compress_on);
if (count != 37)
{
FatherNode.FatherName = L"定位凹点(管端成型)";
ChildrenNode.ChildrenName = vec[0].c_str();
ChildrenNode.ChildrenValue = vec[1].c_str();
FatherNode.items.push_back(ChildrenNode);
}
else if (count == 37)
{
ChildrenNode.ChildrenName = vec[0].c_str();
ChildrenNode.ChildrenValue = vec[1].c_str();
FatherNode.items.push_back(ChildrenNode);
datas.push_back(FatherNode);
FatherNode.FatherName = L"";
FatherNode.items.clear();
}
break;
case 38:case 39:case 40:case 41:
boost::split(vec, labellist[i], boost::is_any_of("<<<"), boost::token_compress_on);
if (count != 41)
{
FatherNode.FatherName = L"定位凸点(管端成型)";
ChildrenNode.ChildrenName = vec[0].c_str();
ChildrenNode.ChildrenValue = vec[1].c_str();
FatherNode.items.push_back(ChildrenNode);
}
else if (count == 41)
{
ChildrenNode.ChildrenName = vec[0].c_str();
ChildrenNode.ChildrenValue = vec[1].c_str();
FatherNode.items.push_back(ChildrenNode);
datas.push_back(FatherNode);
FatherNode.FatherName = L"";
FatherNode.items.clear();
}
break;
case 42:case 43:case 44:case 45:case 46:
boost::split(vec, labellist[i], boost::is_any_of("<<<"), boost::token_compress_on);
if (count != 46)
{
FatherNode.FatherName = L"定位环(管端成型)";
ChildrenNode.ChildrenName = vec[0].c_str();
ChildrenNode.ChildrenValue = vec[1].c_str();
FatherNode.items.push_back(ChildrenNode);
}
else if (count == 46)
{
ChildrenNode.ChildrenName = vec[0].c_str();
ChildrenNode.ChildrenValue = vec[1].c_str();
FatherNode.items.push_back(ChildrenNode);
datas.push_back(FatherNode);
FatherNode.FatherName = L"";
FatherNode.items.clear();
}
break;
case 47:case 48:case 49:case 50:case 51:case 52:
boost::split(vec, labellist[i], boost::is_any_of("<<<"), boost::token_compress_on);
if (count != 52)
{
FatherNode.FatherName = L"扩口(管端成型)";
ChildrenNode.ChildrenName = vec[0].c_str();
ChildrenNode.ChildrenValue = vec[1].c_str();
FatherNode.items.push_back(ChildrenNode);
}
else if (count == 52)
{
ChildrenNode.ChildrenName = vec[0].c_str();
ChildrenNode.ChildrenValue = vec[1].c_str();
FatherNode.items.push_back(ChildrenNode);
datas.push_back(FatherNode);
FatherNode.FatherName = L"";
FatherNode.items.clear();
}
break;
case 53:case 54:case 55:case 56:case 57:case 58:case 59:
boost::split(vec, labellist[i], boost::is_any_of("<<<"), boost::token_compress_on);
if (count != 59)
{
FatherNode.FatherName = L"扩口+凹点(管端成型)";
ChildrenNode.ChildrenName = vec[0].c_str();
ChildrenNode.ChildrenValue = vec[1].c_str();
FatherNode.items.push_back(ChildrenNode);
}
else if (count == 59)
{
ChildrenNode.ChildrenName = vec[0].c_str();
ChildrenNode.ChildrenValue = vec[1].c_str();
FatherNode.items.push_back(ChildrenNode);
datas.push_back(FatherNode);
FatherNode.FatherName = L"";
FatherNode.items.clear();
}
break;
case 60:case 61:case 62:case 63:case 64:case 65:
boost::split(vec, labellist[i], boost::is_any_of("<<<"), boost::token_compress_on);
if (count != 65)
{
FatherNode.FatherName = L"扩口+凹环(管端成型)";
ChildrenNode.ChildrenName = vec[0].c_str();
ChildrenNode.ChildrenValue = vec[1].c_str();
FatherNode.items.push_back(ChildrenNode);
}
else if (count == 65)
{
ChildrenNode.ChildrenName = vec[0].c_str();
ChildrenNode.ChildrenValue = vec[1].c_str();
FatherNode.items.push_back(ChildrenNode);
datas.push_back(FatherNode);
FatherNode.FatherName = L"";
FatherNode.items.clear();
}
break;
case 66:case 67:case 68:case 69:case 70:case 71:
boost::split(vec, labellist[i], boost::is_any_of("<<<"), boost::token_compress_on);
if (count != 71)
{
FatherNode.FatherName = L"缩口(管端成型)";
ChildrenNode.ChildrenName = vec[0].c_str();
ChildrenNode.ChildrenValue = vec[1].c_str();
FatherNode.items.push_back(ChildrenNode);
}
else if (count == 71)
{
ChildrenNode.ChildrenName = vec[0].c_str();
ChildrenNode.ChildrenValue = vec[1].c_str();
FatherNode.items.push_back(ChildrenNode);
datas.push_back(FatherNode);
FatherNode.FatherName = L"";
FatherNode.items.clear();
}
break;
case 72:case 73:case 74:case 75:case 76:case 77:case 78:
boost::split(vec, labellist[i], boost::is_any_of("<<<"), boost::token_compress_on);
if (count != 78)
{
FatherNode.FatherName = L"缩口+凹点(管端成型)";
ChildrenNode.ChildrenName = vec[0].c_str();
ChildrenNode.ChildrenValue = vec[1].c_str();
FatherNode.items.push_back(ChildrenNode);
}
else if (count == 78)
{
ChildrenNode.ChildrenName = vec[0].c_str();
ChildrenNode.ChildrenValue = vec[1].c_str();
FatherNode.items.push_back(ChildrenNode);
datas.push_back(FatherNode);
FatherNode.FatherName = L"";
FatherNode.items.clear();
}
break;
case 79:case 80:case 81:case 82:case 83:case 84:
boost::split(vec, labellist[i], boost::is_any_of("<<<"), boost::token_compress_on);
if (count != 84)
{
FatherNode.FatherName = L"缩口+凹环(管端成型)";
ChildrenNode.ChildrenName = vec[0].c_str();
ChildrenNode.ChildrenValue = vec[1].c_str();
FatherNode.items.push_back(ChildrenNode);
}
else if (count == 84)
{
ChildrenNode.ChildrenName = vec[0].c_str();
ChildrenNode.ChildrenValue = vec[1].c_str();
FatherNode.items.push_back(ChildrenNode);
datas.push_back(FatherNode);
FatherNode.FatherName = L"";
FatherNode.items.clear();
}
break;
case 85:case 86:case 87:case 88:case 89:case 90:case 91:case 92:case 93:
boost::split(vec, labellist[i], boost::is_any_of("<<<"), boost::token_compress_on);
if (count != 93)
{
FatherNode.FatherName = L"二次缩口(管端成型)";
ChildrenNode.ChildrenName = vec[0].c_str();
ChildrenNode.ChildrenValue = vec[1].c_str();
FatherNode.items.push_back(ChildrenNode);
}
else if (count == 93)
{
ChildrenNode.ChildrenName = vec[0].c_str();
ChildrenNode.ChildrenValue = vec[1].c_str();
FatherNode.items.push_back(ChildrenNode);
datas.push_back(FatherNode);
FatherNode.FatherName = L"";
FatherNode.items.clear();
}
break;
case 94:case 95:case 96:case 97:case 98:case 99:case 100:case 101:
boost::split(vec, labellist[i], boost::is_any_of("<<<"), boost::token_compress_on);
if (count != 101)
{
FatherNode.FatherName = L"翻边孔(管端成型)";
ChildrenNode.ChildrenName = vec[0].c_str();
ChildrenNode.ChildrenValue = vec[1].c_str();
FatherNode.items.push_back(ChildrenNode);
}
else if (count == 101)
{
ChildrenNode.ChildrenName = vec[0].c_str();
ChildrenNode.ChildrenValue = vec[1].c_str();
FatherNode.items.push_back(ChildrenNode);
datas.push_back(FatherNode);
FatherNode.FatherName = L"";
FatherNode.items.clear();
}
break;
case 102:case 103:case 104:case 105:case 106:case 107:
boost::split(vec, labellist[i], boost::is_any_of("<<<"), boost::token_compress_on);
if (count != 107)
{
FatherNode.FatherName = L"钻孔(管端成型)";
ChildrenNode.ChildrenName = vec[0].c_str();
ChildrenNode.ChildrenValue = vec[1].c_str();
FatherNode.items.push_back(ChildrenNode);
}
else if (count == 107)
{
ChildrenNode.ChildrenName = vec[0].c_str();
ChildrenNode.ChildrenValue = vec[1].c_str();
FatherNode.items.push_back(ChildrenNode);
datas.push_back(FatherNode);
FatherNode.FatherName = L"";
FatherNode.items.clear();
}
break;
case 108:
boost::split(vec, labellist[i], boost::is_any_of("<<<"), boost::token_compress_on);
FatherNode.FatherName = L"直口去内毛刺(管端成型)";
ChildrenNode.ChildrenName = vec[0].c_str();
ChildrenNode.ChildrenValue = vec[1].c_str();
FatherNode.items.push_back(ChildrenNode);
datas.push_back(FatherNode);
FatherNode.FatherName = L"";
FatherNode.items.clear();
dataArray.push_back(datas);
datas.clear();
break;
default:
break;
}
}
#pragma endregion
return 0;
}
/*静态检查规范*/
static int Excel_Read_Check(vector<LixbXmlFatherNode> &dataArray)
{
ProPath wtext_path;
CString strtext_path;
ProToolkitApplTextPathGet(wtext_path);
strtext_path = (CString)wtext_path;
#pragma region 调用com组件
CoInitialize(NULL);
CLSID clsidr;
ExcelAction::IExcelRead *ptr;
HRESULT hr = CLSIDFromProgID(OLESTR("ExcelAction.ExcelRead_Check"), &clsidr);
if (hr != S_OK)
{
MessageBoxShow(L"未能找到CLSID,检测注册表中名称是否正确.是否注册成功!");
return 0;
}
hr = CoCreateInstance(clsidr, NULL, CLSCTX_INPROC_SERVER, __uuidof(ExcelAction::IExcelRead), (LPVOID*)&ptr);
if (hr != S_OK)
{
MessageBoxShow(L"创建接口对象失败!");
return 0;
}
_bstr_t dirpath = (_bstr_t)strtext_path;
_bstr_t filepath = L"\\管路参数表\\静态检查规范.xlsx";
CComBSTR strr;
strr.AppendBSTR(dirpath);
strr.AppendBSTR(filepath);
BSTR fullpath = strr.m_str;
ptr->ReadExcel(fullpath);
CoUninitialize();
#pragma endregion
#pragma region 读取txt
vector<string> labellist;
ifstream ifs;
CString str_Path;
str_Path.GetEnvironmentVariableW(L"Temp");
ifs.open(str_Path + L"\\StaticCheckSpecification.txt");
string str;
while (getline(ifs, str))
{
labellist.push_back(str);
}
ifs.close();
LixbXmlFatherNode FatherNode;
LixbXmlChildrenNode ChildrenNode;
for (size_t i = 0; i < labellist.size(); i++)
{
int count = i % 6;
switch (count)
{
case 0:
FatherNode.FatherName = labellist[i].c_str();
ChildrenNode.ChildrenName = L"#";
ChildrenNode.ChildrenValue = labellist[i].c_str();
FatherNode.items.push_back(ChildrenNode);
break;
case 1:
ChildrenNode.ChildrenName = L"检查项说明";
ChildrenNode.ChildrenValue = labellist[i].c_str();
FatherNode.items.push_back(ChildrenNode);
break;
case 2:
ChildrenNode.ChildrenName = L"规范要求(示例)";
ChildrenNode.ChildrenValue = labellist[i].c_str();
FatherNode.items.push_back(ChildrenNode);
break;
case 3:
ChildrenNode.ChildrenName = L"检查输出示例";
ChildrenNode.ChildrenValue = labellist[i].c_str();
FatherNode.items.push_back(ChildrenNode);
break;
case 4:
ChildrenNode.ChildrenName = L"备注";
ChildrenNode.ChildrenValue = labellist[i].c_str();
FatherNode.items.push_back(ChildrenNode);
ChildrenNode.ChildrenName = L"";
ChildrenNode.ChildrenValue = L"";
FatherNode.items.push_back(ChildrenNode);
dataArray.push_back(FatherNode);
FatherNode.FatherName = L"";
FatherNode.items.clear();
break;
default:
break;
}
}
#pragma endregion
return 0;
}
};