Excel格式行

时间:2015-12-15 14:11:13

标签: c# excel

我将创建一个Excel文件,其中包含姓名,日期和代码,用于"我的"私人竞选。

它工作正常,但看起来不太好。如何格式化单元格以读取插入日期和文本? 根本无法读取名称和内容 Excel sample screenshot

Excel.Application _xlApp = new Excel.Application();
    Excel.Workbook _xlWorkBook;
    Excel.Worksheet _xlWorkSheet;
    object misValue = System.Reflection.Missing.Value;

    internal void CreateExcelFile(string writePath)
    {
        this.CreateHeader();
        this.AddContent();
        this.SaveFile(writePath);
    }
    private void CreateHeader()
    {
        _xlWorkBook = _xlApp.Workbooks.Add(misValue);
        _xlWorkSheet = (Excel.Worksheet)_xlWorkBook.Worksheets.get_Item(1);

        _xlWorkSheet.Cells[1, ExcelObject.SAMNAME] = "SAM_NAME";
        _xlWorkSheet.Cells[1, ExcelObject.DATE] = "Date";
        _xlWorkSheet.Cells[1, ExcelObject.CODE] = "Code";
        FormatCells(_xlWorkSheet.Cells.Borders);
    }
    private void AddContent()
    {
        int i = 1;
        ++i;
        _xlWorkSheet.Cells[i, ExcelObject.Date] = DateTime.Now.ToString("yyyyMMdd_HHmmss");
        _xlWorkSheet.Cells[i, ExcelObject.SAMNAME] = "Julek";

    }
    private void FormatCells(Excel.Borders _borders)
    {
        _borders[Excel.XlBordersIndex.xlEdgeLeft].LineStyle = Excel.XlLineStyle.xlContinuous;
        _borders[Excel.XlBordersIndex.xlEdgeRight].LineStyle = Excel.XlLineStyle.xlContinuous;
        _borders[Excel.XlBordersIndex.xlEdgeTop].LineStyle = Excel.XlLineStyle.xlContinuous;
        _borders[Excel.XlBordersIndex.xlEdgeBottom].LineStyle = Excel.XlLineStyle.xlContinuous;
        _borders.Color = ConsoleColor.Black;
    }
    private void SaveFile(string writePath)
    {
        string fileName = string.Format("{0}_{1}.xls","SampleFile", DateTime.Now.ToString("yyyyMMdd_HHmmss"));
        _xlWorkBook.SaveAs(Path.Combine(writePath, fileName), Excel.XlFileFormat.xlWorkbookNormal, misValue,
           misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);
        _xlWorkBook.Close(true, misValue, misValue);
        _xlApp.Quit();

        ReleaseObject(_xlWorkSheet);
        ReleaseObject(_xlWorkBook);
        ReleaseObject(_xlApp);
    }
    private void ReleaseObject(object obj)
    {
        try
        {
            System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
            obj = null;
        }
        catch (Exception) { throw; }
        finally
        {
            GC.Collect();
        }
    }

1 个答案:

答案 0 :(得分:1)

指定整列的格式并使用ToOADate()方法代替.ToString("yyyyMMdd_HHmmss")

// get the date cell
Range rg = _xlWorkSheet.Cells[ExcelObject.DATE, ExcelObject.DATE];
// specify the format for the whole column
rg.EntireColumn.NumberFormat = @"yyyyMMdd\_HHmmss";

// ...

//  write the current date
_xlWorkSheet.Cells[i, ExcelObject.DATE] = DateTime.Now.ToOADate();

我已将格式从yyyyMMdd_HHmmss更改为yyyyMMdd\_HHmmss,因为Excel会将_转换为空格,而\会保留下划线。

可以通过这种方式自动更改列的宽度(在编辑结束时调用此方法):

rg.EntireColumn.AutoFit();

插入所有数据时,最后应调用AutoFit方法。它只在调用时改变宽度一次,但不是连续的。