【问题标题】:How to specify cell format to 'Text' when export Excel file [duplicate]导出Excel文件时如何将单元格格式指定为“文本”[重复]
【发布时间】:2015-11-09 05:11:26
【问题描述】:

当我将数据添加到数据表时,我正在尝试使用 .ToString() 导出 Excel 文件。 Excel 文件单元格格式不是文本,而是General 格式。这是我的代码。

public static void CreateExcel(string filename,System.Data.DataTable table)
{
    if (filename != "")
    {
        Application xlApp = new Microsoft.Office.Interop.Excel.Application();
        xlApp.DisplayAlerts = false;

        if (xlApp == null)
        {
            return;
        }

        Workbook xlWorkBook;
        Worksheet xlWorkSheet;
        object misValue = System.Reflection.Missing.Value;

        xlWorkBook = xlApp.Workbooks.Add(misValue);
        xlWorkSheet = (Worksheet)xlWorkBook.Worksheets.get_Item(1);

        int Row = table.Rows.Count;
        int Column = table.Columns.Count;

        for (int i = 0; i <= Column - 1; i++)
        {
            xlWorkSheet.Cells[1, i + 1] = table.Columns[i].ToString();
        }

        for (int i = 0; i <= Row - 1; i++)
        {
            for (int j = 0; j <= Column - 1; j++)
            {
                xlWorkSheet.Cells[i + 2, j + 1] = table.Rows[i][j].ToString();
            }
        }

        xlWorkBook.SaveAs(@filename, Microsoft.Office.Interop.Excel.XlFileFormat.xlOpenXMLWorkbook, System.Reflection.Missing.Value, misValue, false, false, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange, Microsoft.Office.Interop.Excel.XlSaveConflictResolution.xlUserResolution, true, misValue, misValue, misValue);

        xlWorkBook.Close();
        xlApp.Quit();
        Marshal.FinalReleaseComObject(xlWorkSheet);
        Marshal.FinalReleaseComObject(xlWorkBook);
        Marshal.FinalReleaseComObject(xlApp);

        GC.Collect();
    }
}

有没有办法改变单元格格式?

【问题讨论】:

  • 当您在 Excel UI 中编辑单元格时,您可以通过以单引号 (') 字符开头的值来强制文本格式。也许这在由代码设置时也有效?
  • @modal_dialog 它对我有用。想

标签: c# excel excel-interop


【解决方案1】:

设置单元格的数字格式将帮助您将单元格类型转换为文本。

Excel.Worksheet ws = workBook.Worksheets[1];
ws.Range["A2"].NumberFormat = "0";

【讨论】:

    【解决方案2】:
        // Pull in all the cells of the worksheet
        Range cells = xlWorkBook.Worksheets[1].Cells;
        // set each cell's format to Text
        cells.NumberFormat = "@";
    

    这里有 2 个链接可以在各种情况下为您提供帮助。 Link 1 & Link 2

    在你的情况下:

    for (int i = 0; i <= Row - 1; i++)
            {
                for (int j = 0; j <= Column - 1; j++)
                {
                    xlWorkSheet.Cells[i + 2, j + 1] = table.Rows[i][j].ToString();
                   //It will set each cell while iterates, 
                   //I suggest, if its for all column, then set it for the excel sheet. 
                   //or if you have a known range, then set for the range. 
                    xlWorkSheet.Cells[i + 2, j + 1].NumberFormat = "@";
                }
            }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-04-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-22
      相关资源
      最近更新 更多