在C#中使用过去“Z”的字符数

我有一个数据集转储使用一个字符集types循环find第一列字母放入英寸这对伟大的亚利桑那州,但是当我需要AA,或AB,我不知所措。 这是我正在使用的,从我在网上find的一些代码重做。 我可以做任何事吗?

public void ExportToExcel(DataSet dataSet, string templatePath, int i, int h) { Excel.Application excelApp = new Excel.Application(); excelApp.Visible = true; FileInfo excelFileInfo = new FileInfo(templatePath); Boolean fileOpenTest = IsFileLocked(excelFileInfo); Excel.Workbook templateBook; if (!fileOpenTest) { templateBook = excelApp.Workbooks.Open(templatePath); } else { templateBook = (Excel.Workbook)System.Runtime.InteropServices.Marshal.BindToMoniker(templatePath); } string tabName = lstQueryDumpSheet.Items[i].ToString(); Excel.Worksheet templateSheet = templateBook.Sheets[tabName]; // Copy DataTable foreach (System.Data.DataTable dt in dataSet.Tables) { // Copy the DataTable to an object array object[,] rawData = new object[dt.Rows.Count + 1, dt.Columns.Count]; // Copy the values to the object array for (int col = 0; col < dt.Columns.Count; col++) { for (int row = 0; row < dt.Rows.Count; row++) { rawData[row, col] = dt.Rows[row].ItemArray[col]; } } // Calculate the final column letter string finalColLetter = string.Empty; string colCharset = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; int colCharsetLen = colCharset.Length; if (dt.Columns.Count > colCharsetLen) { finalColLetter = colCharset.Substring((dt.Columns.Count - 1) / colCharsetLen - 1, 1); } finalColLetter += colCharset.Substring((dt.Columns.Count - 1) % colCharsetLen, 1); // Fast data export to Excel string dumpCellString = lstQueryDumpText.Items[i].ToString(); string dumpCell = dumpCellString.Split('=').Last(); string excelRange = string.Format(dumpCell + ":{0}{1}", finalColLetter, dt.Rows.Count + 1); templateSheet.get_Range(excelRange, Type.Missing).Value2 = rawData; } 

您可以使用Excel将列索引转换为字母,反之亦然。

这里有一些VBA代码来做到这一点。 你可以适应c#:

 ' Letter (AA) to index (27)... MsgBox Sheet1.Columns("AA").Column ' Index (27) to letter (AA)... MsgBox Mid$(Sheet1.Columns(27).Address, 2, 2)