c# - what is next cell column in Excel - combinations/permutations -
I need to know the column name of a spreadsheet that is in a column column with a column, AB " , After 99 columns, I want to know the column.
For those who need Ralph to respond in T-SQL taste
FUNCTION fsExcelValueToColum (@value int) Worker (3) as DECLARE @ digit INT, RV VARCHAR (3); set @ RV = 'if @ value = 0 returns @ rv WHEY @VALUE & gt; 0 BEGIN SET @ DIGIT = @ VALUE% 26 IF @DIGIT = 0 from BEGIN @ RV = RV + 'Z' set @ RV = RV + deb. Faxexelwoltogolum (@value / 26 -1) return @ RV ed set @ RV = @ R .V + Charge (@ digit + 64) set @value = (@ value- @ points) / 26nd return @ RV and use [ECS] Go / ****** Object: User defined function [DBO]. [Fnaxel Column to Volume] Script Date: 12/06/2009 10:33:37 ****** / SET ANSI_NULLS Set To Go Go Changes Option [DBO]. [Fnaxel Column To Value] (@column Varchar (3)) sh Second as does the whole --declare @ pen varchar (10); @column = declare 'AC' @value int, @pow int, @ i int, @ n int select @ pow = 1, @ VALUE = 0, @ I = LEN (@COLUMN) SET @ I = LEN (@COLUMN ) WHILE @ I & gt; = 1 BEGIN SET @ VALUE = @ VALUE + @ POW * (ASCII (SUBSTRING (UPPER (@COLUMN), @ I, 1)) - 65) +1) SE @ POW = @ POW * 26 SET @ I = @ I -1 END return value @
Think about the letter A ,
b
, ..., z
(you can not think enough about having this base-26 Because none of the characters represent 0 nor do we want to do this, for example, aa
means 0). Therefore, we have to engage in some invoices to deal with it:
Fixed integer column column (string column) {int value = 0; Int pow = 1; For (int i = column.Length - 1; i> = 0; i--) {value + = pow * (column [i] - 'A' + 1); Pau * = 26; } return value; } Fixed String ValueToColumn (int value) {if (value == 0) {return string.Empty; } Stringbiller sb = New stringbilder (); While (value> gt; 0) {int numeral = value% 26; If (number == 0) {sb.Insert (0, 'Z'); Return sb.Insert (0, ValueToColumn (Value / 26 - 1)). ToString (); } Sb.Insert (0, (four) (number + 'a' - 1)); Value = (value - number) / 26; } Return sb.ToString (); }
is that
console. WrightLine (ValueToColumn (ColumnToValue ("AB") + 99);
Output DW
and
console. WrightLine (ValueToColumn (ColumnToValue ("AB") - 2));
Output Z
. Obviously you can wrap up all this in a good orbit and make it fluent or what you can do. Explanation: For example, for example, representing the value 702 and a normal base-26 marking where there is a digit 0 (I use the notation _
this magical To represent the digits so that we do not get confused between value 0 and number 0
) Let's try to convert 702 (decimal) to base-26. The normal algorithm is calculated to 702% 26, which is 0 so that we have the previous code as _
. Then we will divide 26 to 27 parts. We will note that 27% is 26 1, so that the last digit is a
. Then we will take part from 26 to get 1, calculate 1% 26 to get 1 and report that the most important digit is A
we get the aa_
Returning to the form of a string - representing the digits (70) in base-26 with numbers ( _
= 0, a
= 1, ... < Code> Z = 26) (Check: 1 * 26 ^ 2 + 1 * 26 ^ 1 + 0 * 26 = 702) For our purposes, this is wrong. We wanted to get back the string ZZ
(because we have 26 digits a
, B
, C
,. . Z
1, 2, ... 26 represent (remember, no number representing the value 0!) So that zz
= 26 * 26 ^ 1 + 26 * 26 ^ 0 = 702) It is found in our system that the least significant number of numbers corresponding to the module 26 should be Z
so it is found that checking our algorithm And should see that value
0 corresponds to modulo 26. If so, then prepaid a Z
and then represent the value of the string value / 26 - 1 < / Code>. This is the algorithm that you see above.
Comments
Post a Comment