计算EXCEL列名代号的程序

本文介绍了一个使用C#实现的程序,该程序能够生成Excel中使用的列名代号,从A到ZZ,再到AAA等。通过递增方式模拟了Excel列标的创建过程。
None.gif // 计算结果格式:
None.gif        
// A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z,
None.gif        
// AA,AB,AC,AD,AE,AF,AG,AH,AI,AJ,AK,AL,AM,AN,AO,AP,AQ,AR,AS,AT,AU,AV,AW,AX,AY,AZ,
None.gif        
// BA,BB,BC,BD,BE,BF,BG,BH,BI,BJ,BK,BLdot.gifdot.gif
None.gif
         private   void  buttonXP1_Click( object  sender, System.EventArgs e)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif            
string s="A";
InBlock.gif            
for(int i=0;i<2700;i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
this.richTextBox1.Text +=s+",";
InBlock.gif                s
=InreasingData(s);
InBlock.gif                 
ExpandedSubBlockEnd.gif            }

InBlock.gif          MessageBox.Show(s)  ;
InBlock.gif
ExpandedBlockEnd.gif        }

None.gif        
private   string  InreasingData( string  strTemp)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif            
int i=strTemp.Length ;
InBlock.gif            
char[] ca=new char[i];
InBlock.gif            ca
=strTemp.ToCharArray();  
InBlock.gif            
bool bCarryFlag=true;
InBlock.gif            
int w=i-1;
InBlock.gif        
InBlock.gif            
int AddResult=(int)ca[w];
InBlock.gif            AddResult
+=1;
InBlock.gif
InBlock.gif        
InBlock.gif            
while((bCarryFlag)&&(w>=0))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
InBlock.gif                
if(AddResult>90)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    ca[w]
='A';    
InBlock.gif                    
InBlock.gif                    
InBlock.gif                    
if(w>0)
InBlock.gif                    AddResult
=(char)((int)ca[w-1]+1);
InBlock.gif
InBlock.gif                
InBlock.gif                
InBlock.gif                    bCarryFlag
=true;
ExpandedSubBlockEnd.gif                }

InBlock.gif                
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    ca[w]
=(char)((int)ca[w] +1);
InBlock.gif                    bCarryFlag
=false;
ExpandedSubBlockEnd.gif                }

InBlock.gif                w
--;
InBlock.gif                    
ExpandedSubBlockEnd.gif            }

InBlock.gif            strTemp
="";
InBlock.gif            
for(int j=0;j<i;j++)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                strTemp
+=ca[j].ToString(); 
ExpandedSubBlockEnd.gif            }

InBlock.gif            
if((w<0)&&(bCarryFlag))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                strTemp
="A"+strTemp;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
InBlock.gif            
return strTemp;
ExpandedBlockEnd.gif        }
ExpandedBlockStart.gif ContractedBlock.gif Private   Sub ButtonXP1_Click() Sub ButtonXP1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonXP1.Click
InBlock.gif        
Dim i As Int16 = 0
InBlock.gif        
Dim s As String = "A"
InBlock.gif        
Me.RichTextBox1.Text &= s & "',"
InBlock.gif        
For i = 0 To 1000
InBlock.gif            s 
= IncreasingData(s)
InBlock.gif            
Me.RichTextBox1.Text &= s & "',"
InBlock.gif        
Next
InBlock.gif
InBlock.gif
ExpandedBlockEnd.gif    
End Sub

None.gif
ExpandedBlockStart.gifContractedBlock.gif    
Private   Function IncreasingData() Function IncreasingData(ByVal strTemp As StringAs String
InBlock.gif        
Dim i As Integer = strTemp.Length
InBlock.gif        
Dim ca(i) As Char
InBlock.gif        ca 
= strTemp.ToCharArray()
InBlock.gif        
Dim bCarryFlag As Boolean = True
InBlock.gif        
Dim w As Integer = (i - 1)
InBlock.gif        
Dim AddResult As Integer = Microsoft.VisualBasic.AscW(ca(w))
InBlock.gif        AddResult 
+= 1
InBlock.gif        
While ((bCarryFlag) And (w >= 0))
InBlock.gif            
If (AddResult > 90Then
InBlock.gif                ca(w) 
= "A"
InBlock.gif                
If (w > 0Then
InBlock.gif                    AddResult 
= Microsoft.VisualBasic.AscW(ca(w - 1)) + 1
InBlock.gif                    bCarryFlag 
= True
InBlock.gif                
End If
InBlock.gif            
Else
InBlock.gif                ca(w) 
= Microsoft.VisualBasic.ChrW(Microsoft.VisualBasic.AscW(ca(w)) + 1)
InBlock.gif                bCarryFlag 
= False
InBlock.gif            
End If
InBlock.gif            w 
= w - 1
InBlock.gif        
End While
InBlock.gif        strTemp 
= ""
InBlock.gif        
Dim j As Integer = 0
InBlock.gif        
For j = 0 To i - 1
InBlock.gif            strTemp 
&= ca(j).ToString()
InBlock.gif        
Next
InBlock.gif        
If ((w < 0And (bCarryFlag)) Then
InBlock.gif            strTemp 
= "A" & strTemp
InBlock.gif        
End If
InBlock.gif        
Return strTemp
ExpandedBlockEnd.gif    
End Function
posted on 2006-06-08 17:10 DotNet编程 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/furenjun/archive/2006/06/08/420833.html

任务描述: 第23题分配学号 文件schoolCode.csv 和MajorCode.csv 中的数据是每个学院的编号和专业的编号,studentList.csv文件中有 若干学生信息,学生出现的顺序是他在班级中的排名顺序,每行中的数据用逗号分隔,各数据依顺代表: 学生姓名,学生性别,学院,专业名称,行政班(专业加班级号,例如经济1901),入学年级。 假如本科的学生的层次编号为012,请为studentList.csv中的数据增加学号、学号创建规则是:学生层次+ 入学年份后两位+学院代号+专业代号+班级号+班中排名。 例如:012171985170110表示本科生,2017年入学,文法学院。编辑出版专业,1701班,排名为10的同 学。 输入: 第一行输入学生姓名 第二行输入班级 输出: 输出其学号和学生姓名,学生性别,学院,专业名称,行政班,入学年级信息,各项之间用空格分隔 输出该班所有同学的信息 分析: 1、csv文本文件格式以及该格式文件数据读取操作,分别获取学生信息列表、学院信息列表和专业信息列 表: student_list = read_file('./data/studentList.csv')[1:] school_code = read_file('./data/schoolCode.csv') major_code = read_file('/data/MajorCode.csv') #获得学生信息列表,去掉标题行 #获得学院信息列表 #获得专业信息列表 2、定义student_id()函数,根据学生信息列表、学院信息列表和专业信息列表构建该学生的学号,并返回 学生的信息。 def student_id(ls_student, Is_school, Is_major): “接收学生列表、学院列表和专业列表为参数,生成学号并与学生信息合并为一个包含学生详细信息 的列表,返回学生详细信息列表。 @参数ls_student:学生信息列表,列表类型 @参数 Is_school:学院信息列表,列表类型 @参数Is_major:专业信息列表,列表类型 dic_school={x[0]: x[1] for x in Is_school] dic _major ={x[0]:x[1] for x in Is_major} detail =[] #构建学院字典 for student in Is student: #构建专业字典 student_number='012' #生成学号 student _number = student_number + student[5][2:]+ dic_school[student[2]]+ dic_major[student[3]]+ student[4][-4:] # student[5][2:]入学年份后2位,student[2]是学院,dic_school[student[2]]是学院编号, #student[3]是专业,dic_major[student[3]]是专业编号,student[4]是专业班级,如”新闻类1703', # student[4][-4:]取班级编号后2位 student_number = student_number +'{0:0>2}'.format([x[0] for x in Is_student if student[4]== x[4]].index(student[0])+ 1) #查询学生在自己班级中的排序拼接到学号上 36 detail.append([student_number]+ student) return detail #将学号和其他信息加入到列表中 重要知识点: #返回加了学号的学生信息 1、模块化程序设计方法: 2、函数的定义与调用; 3、csv文件的读取: 4、字典操作; 5、列表操作。我将文件放到了桌面上另存为UTF-8编码的csv,
06-13
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值