7-1 Excel列名和列序号转换

本文介绍了一个C++程序,该程序能够将包含字母和数字的字符串转换为对应的数值表示,并能逆向将数值转换回字符串形式。当字符串中出现字母时,采用26进制进行转换;若全是数字,则直接按十进制转换。

在这里插入图片描述
Z 为26 和前一位的A表示相同
注意 index数组放在main外边过不了,显示编译错误。

#include<bits/stdc++.h>
using namespace std;
int main()
{
	string a;
	int flag=0;
	while(cin>>a)
	{
		if(a=="#")
		 break;
		char index[105];
		flag=0;
		int cnt=0,len=a.length();
		long long int num=0;
		for(int i=0;i<len;i++)
		{
			if(a[i]>='0'&&a[i]<='9')
			 num=num*10+a[i]-'0';
			else
			{
				num=num*26+a[i]-'A'+1;
				flag=1;
			}
		}
		if(flag) printf("%lld\n",num);
		else
		{
			while(num)
			{
				if(num%26==0)
				{
					index[cnt]=26+64;//A是65
					cnt++;
					num=num/26;
					num--; 
				}
				else
				{
					index[cnt]=num%26+64;
					cnt++;
					num/=26;
				}
			}
			for(int i=cnt-1;i>=0;i--)
			 printf("%c",index[i]);
			printf("\n");
		}
	}
	return 0;
 } 
#region 读取"粗糙度检测Csv"(DataTable) public void GetCsvData(string filePath) { try { if (!File.Exists(filePath)) { throw new Exception("文件路径不存在:"+ filePath); } DataTable table = new DataTable(); // 使用StreamReader读取CSV文件 DataSet ds; Dictionary<string, string> csvData = new Dictionary<string, string>(); using (var reader = new StreamReader(filePath, Encoding.UTF8)) { // 读取第一行并分割到数组 string firstLine = reader.ReadLine(); string[] firstArray = firstLine.Split(','); // 处理空行情况 // 读取第二行并分割到数组 string secondLine = reader.ReadLine(); string[] secondArray = secondLine.Split(','); // 处理空行情况 // 使用示例:打印结果 Scripter.ShowMessage("第一行数据:"+ firstArray); Scripter.ShowMessage("第二行数据:"+ secondArray); //读取csv的3-18行(索引2-17) for(int i=2;i<=17;i++){ //第一行是检验项(索引0),第二行是检验值(索引1) string itemName = firstArray[i]; string actualValue = secondArray[i]; Scripter.ShowMessage("读取列名:"+ itemName); Scripter.ShowMessage("读取值:"+ actualValue); if (!string.IsNullOrEmpty(itemName)) { csvData[itemName] = actualValue; } } ds = (DataSet)CheckTheDetailGrid.DataSource; } // 根据检验项名称匹配网格数据 foreach (DataRow row in ds.Tables[0].Rows) { string gridItemName = row["ItemName"].ToString().Trim(); if (csvData.ContainsKey(gridItemName)) { // Scripter.ShowMessage(row[0].ToString()); // Scripter.ShowMessage(row[1].ToString()); // Scripter.ShowMessage(row[2].ToString()); // Scripter.ShowMessage(gridItemName); row["ActualValue"] = csvData[gridItemName]; // 自动进行合格判定 double actualValue = Convert.ToDouble(csvData[gridItemName]); double minValue = Convert.ToDouble(row["MinValue"]); double maxValue = Convert.ToDouble(row["MaxValue"]); if (actualValue >= minValue && actualValue <= maxValue) { row["CheckResult"] = "合格"; } else { row["CheckResult"] = "不合格"; } } for (int rowIndex = 0; rowIndex < CheckTheDetailGrid.Rows.Count; rowIndex++) { UltraGridRow gridRow = CheckTheDetailGrid.Rows[rowIndex]; if (gridRow.Cells["CheckResult"].Value != null && gridRow.Cells["CheckResult"].Value.ToString() == "不合格") { gridRow.Cells["CheckResult"].Appearance.BackColor = Color.Red; } else { gridRow.Cells["CheckResult"].Appearance.BackColor = Color.White; } } } } catch (Exception ex) { Scripter.ShowMessage(string.Format("GetCsvData:{0}", ex.Message), true); } } #endregion这个是我读取csv的代码,其中csv内容为序号,测试个数,粗糙度-1,粗糙度-2,粗糙度-3,粗糙度-4,粗糙度-5,粗糙度-6,粗糙度-7,粗糙度-8,粗糙度-9,粗糙度-10,粗糙度-11,粗糙度-12,粗糙度-13,粗糙度-14,粗糙度-15,粗糙度-16, 25VFE2160,4,5.935,4.390,5.040,5.487,0.000,0.000,0.000,0.000,0.000,0.000,0.000,0.000,0.000,0.000,0.000,0.000, 输出为:46:40 - 读取文件路径:D:\粗糙度检测报告\2025年\11月\11.10\25H26PP4952.csv 16:46:40 - 第一行数据:System.String[] 16:46:40 - 第二行数据:System.String[] 16:46:40 - 读取列名:�ֲڶ�-1 16:46:40 - 读取值:5.935 16:46:40 - 读取列名:�ֲڶ�-2 16:46:40 - 读取值:4.390 16:46:40 - 读取列名:�ֲڶ�-3 16:46:41 - 读取值:5.040 16:46:41 - 读取列名:�ֲڶ�-4 16:46:41 - 读取值:5.487 16:46:41 - 读取列名:�ֲڶ�-5 16:46:41 - 读取值:0.000 16:46:41 - 读取列名:�ֲڶ�-6 16:46:41 - 读取值:0.000 16:46:41 - 读取列名:�ֲڶ�-7 16:46:41 - 读取值:0.000 16:46:41 - 读取列名:�ֲڶ�-8 16:46:41 - 读取值:0.000 16:46:41 - 读取列名:�ֲڶ�-9 16:46:41 - 读取值:0.000 16:46:41 - 读取列名:�ֲڶ�-10 16:46:41 - 读取值:0.000 16:46:41 - 读取列名:�ֲڶ�-11 16:46:41 - 读取值:0.000 16:46:41 - 读取列名:�ֲڶ�-12 16:46:41 - 读取值:0.000 16:46:41 - 读取列名:�ֲڶ�-13 16:46:41 - 读取值:0.000 16:46:41 - 读取列名:�ֲڶ�-14 16:46:41 - 读取值:0.000 16:46:41 - 读取列名:�ֲڶ�-15 16:46:41 - 读取值:0.000 16:46:41 - 读取列名:�ֲڶ�-16 16:46:41 - 读取值:0.000分析原因
最新发布
11-11
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值