如何设置日期格式“dd / mm / yyyy”在excel一列中,所有行都使用C#npoi。 [英] How to set date format "dd/mm/yyyy" in excel one column with all rows using C# npoi.

查看:562
本文介绍了如何设置日期格式“dd / mm / yyyy”在excel一列中,所有行都使用C#npoi。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,



我正在尝试设置日期格式但不成功,请帮助我如何设置一列所有行的日期格式,如列A 。



我的excel就像一个模板,excel中没有数据导出,只有格式会用excel导出。



我也想锁定这种格式。



先谢谢。



Ankit Agarwal

软件工程师



我尝试过:



Hello,

I am trying set Date format but not successful, please help me how will be set date format for one column with all rows like column "A".

My excel like a template, no data export in excel, only format will be export with excel.

I want to lock this format also.

Thanks in Advance.

Ankit Agarwal
Software Engineer

What I have tried:

public static string ExportTemplateToExcel()
        {
            string CreatedDateTime = "Start Date";
            //String file = "c:\\poitest.xlsx";
            string TempPath = "c:\\Testing\\";
            string Filename = "Test.xlsx";


            IWorkbook workbook = new XSSFWorkbook();

            ISheet sheet = workbook.CreateSheet();
            //sheet.ProtectSheet("password");
            IRow row = sheet.CreateRow(0);
            //ICell cell = row.CreateCell(0);
            //cell.SetCellValue("TEST");

            ICellStyle style1 = workbook.CreateCellStyle();

            
            IDataFormat dataFormatCustom = workbook.CreateDataFormat();
            //row2.CreateCell(2).SetCellValue(CreatedDateTime);
            style1.DataFormat = dataFormatCustom.GetFormat("dd/MM/yyyy");
            var datecell = row.CreateCell(0);
            datecell.CellStyle = style1;

            datecell.CellStyle.IsLocked = false;
            datecell.SetCellValue(CreatedDateTime);
            datecell.CellStyle.IsLocked = true;
        
            ISheet sheet1 = workbook.CreateSheet("Sheet1");
            IRow row1 = sheet1.CreateRow(0);

            string FilePath = TempPath + Filename;
            GC.Collect();
            FileStream xfile = new FileStream(FilePath, FileMode.Create, System.IO.FileAccess.Write, FileShare.None, bufferSize: 4096, useAsync: false);
            workbook.Write(xfile);
            return ("Success");

        }

推荐答案

你正在创建一个模板。



创建一个模板excel book / sheet和重用。



使用代码执行此操作是毫无意义和冗余的..因为你最初必须至少创建一个模板;最好使用Excel。
You're creating a "template".

Create a "template excel book / sheet" and "reuse" that.

Using code to do this is pointless and redundant ... since you always have to create at least ONE template initially; preferably using "Excel".


这篇关于如何设置日期格式“dd / mm / yyyy”在excel一列中,所有行都使用C#npoi。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆