Z后表格列值未在控制台窗口中正确显示

时间:2014-11-17 16:29:28

标签: c# excel ascii

以下代码从Excel电子表格中获取数据,验证来自一组预定义规则的数据,并将任何错误写入控制台。

这很有用。数据按预期返回到Z列。如果返回任何错误,则通过Z.AB,AC,AD等。然后返回值开始搞乱我得到的值,如,],?我认为这个问题归结为ASCII,因为我从65年代(A)开始。我想我需要编写某种方法来处理这个但不知道从哪里开始。非常感谢任何帮助。

namespace WorksheetValidator
{
    public class XcelReader
    {
        private readonly List<List<IRule>> m_Rules;

        public XcelReader(List<List<IRule>> rules)
        {
            m_Rules = rules;
        }

        public void ValidateWorksheet(string fileName)
        {
            bool allRulesPassed = true;

            WorkbookProvider workbookProvider = new WorkbookProvider();

            IWorkbook workbook;

            using (FileStream fileStream = File.OpenRead(fileName))
                workbook = workbookProvider.GetWorkbook(fileStream, SpreadsheetType.Xlsx);

            for (int rowCounter = 1; rowCounter < workbook.Worksheets[1].Rows.Count; rowCounter++)
            {
                IRow row = workbook.Worksheets[1].Rows[rowCounter];

                for (int columnCounter = 0; columnCounter < row.Cells.Count; columnCounter++)
                {
                    List<string> failedRules = ColumnValueIsValid(row.Cells[columnCounter].Value, m_Rules[columnCounter]);

                    failedRules.ForEach(failedRule =>
                    {
                        allRulesPassed = false;
                        Console.WriteLine("\n[{0}:{1}] Failed: {2}", rowCounter + 1, (char)(columnCounter + 65), failedRule);
                    });
                }
            }

            if(allRulesPassed)
                Console.WriteLine("\n\n\nWOOHOO! worksheet is hunky dory");
        }

        private List<string> ColumnValueIsValid(string value, List<IRule> rules)
        {
            List<string> failedRules = new List<string>();

             rules.ForEach(rule =>
             {
                 if(!rule.IsValid(value))
                     failedRules.Add(rule.GetReasonForFailure(value));
             });

            return failedRules;
        }
    }
}

1 个答案:

答案 0 :(得分:0)

替换它:

(char)(columnCounter + 65)

使用将0转换为&#34; A&#34;,1至&#34; B&#34; ..... 26至&#34; AA&#34;,27至&#34的功能; AB&#34;等