转换后的 Excel 文件删除前导零

问题描述 投票:0回答:1

我正在尝试转换包含学生 ID 的 csv 文件。在csv文件中,某些学生ID以0开头(例如00121334)问题是,当我打开转换后的excel文件时,Excel会删除所有前导零。 我的问题是如何保证Excel不会删除所有前导零? 我尝试过使用这个

destintationXLS.NumberFormat = "@";
,但我不太确定该把它放在哪里。

这是我的代码:

public bool FormatInputToXLS(string CSVInputF1le, string XLSOutputFile, out string errormsg)
{
    bool isSuccess = faises
    errorMsg = string.Emptys
    Excel.Application xLApp2 = new Excel.Application();
    try
    {
        if(x1App2 == nu11)
        {
            x1App2 = new Excel.Application
            {
                Visible = true,
                DisplayAlerts = false,
                SheetsInNeuhlorkbook= 1
            };
        }
        bool fileExists =File.Exists(CSVInputFile);
        bool fileExists2 = File.Exists(XLSOutputFile);

        try
        {
            Excel.Workbook x1Book;
            Excel.Workbook x1Book2;
            xlBook = xLApp2.workbooks.Open(CSVInputFile);

            if(fileExists2)
                xlBook2 = xlApp2.Workbooks.Open(xLSOutputFile);
            else
                xlBook2 = xLApp2.Workbooks.Add(missing);
            try
            {
                Excel.Worksheet x1Sheet = (Excel.Worksheet)xlBook.Worksheets.get_Item(1);
                Excel.Worksheet xlSheet2 = (Excel.Worksheet)x1Bool2.Worksheets.get_Item(1);

                xlSheet2.Cells[1, 1] = "Student ID";

                int lastRow = 1;
                if(FileExists)
                {
                    int[] colsToCheck = { 1 };
                    List<int> nonEmpty = new List<int>();
                    for(int col = 0; col < colsToCheck.Length; col++)
                    {
                        nonEmpty.Add(((Excel.Range)x1Sheet.Cells[1, colsToCheck[col]]).End[Excel.X10irection.xlDown].Row);
                    }
                    lastRow = nonEmpty.Min();
                }

                Excel.Range sourceCSV = x1Sheet.get_Range(string.Concat("A1:A",lastRow));
                Excel.Range destintationXLS = x1Sheet2.Range["A2"];
                destintationXLS.NumberFormat = "@";
                sourceCSV.Copy();
                destintationXLS.PasteSpecial(Excel.XlPasteSpecialType.xlPasteValues);

                for(int xlCol = 1; xlCol < xlSheet2.UsedRange.Columns.Count; xlCol++)
                {
                    ((Excel.Range)xlSheet2.Cells[1,xlCol]).EntireColumn.AutoFit();
                }
                if (fileExists2)
                    xlBook2.Save();
                else
                    xlBook2.SaveAs(XLSOutputFile, Excel.XlFileFormat.xlOpenXMLWorkbook, missing,missing,missing,missing,Excel.XlSaveAsAccessMode.xlExclusive,missing,missing,missing,missing,missing);
                    isSuccess = true;
                    xlBook.CLose(0);
                    xlBook2.Close(0);
            }
            catch(Exception e)
            {
                errorMsg = string.Concat("Failed to update file. ", e.Message);
            }
            finally
            {
                xlBook.Close(0);
                xlBook2.Close(0);
            }
        }
        catch(Exception e1)
        {
            if (fileExists)
                errorMsg = string.Concat("Failed to open file. ", e1.Message);
            else    
                errorMsg = string.Concat("Failed to create file. ", e1.Message);
        }
        finally
        {
                CloseExcel(xlApp2)
        }
    }
    catch(Exception e2)
    {
        errorMsg = string.Concat("Failed to open Excel application. ", e2.Message);
    }
    return isSuccess;
}

ps。代码上有一些错字,代码对我来说是有效的。问题是前导零

c# excel csv
1个回答
0
投票

格式对您的情况并没有真正的帮助,因为您需要将基础数据类型更改为文本以锁定前导零。

在下面的屏幕截图中,我应用了数字格式

00000
为 5 位数格式。这将填充前导零,但基础值仍然只是数字
345
,正如您在 A1 的公式栏中看到的那样。

如果您想获得带前导零的真实文本值,可以使用 TEXT() 函数。我已将此公式应用于单元格 A3:

=TEXT(A2,"00000")

您可以在新的辅助列中以编程方式应用此函数,然后复制结果并使用“选择性粘贴”>“值”粘贴到原始数字列。之后,您可以删除辅助列。

© www.soinside.com 2019 - 2024. All rights reserved.