Click here to Skip to main content
15,894,896 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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");

        }
Posted
Updated 13-Jun-18 4:38am
v3

1 solution

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".
 
Share this answer
 
Comments
Maciej Los 12-Jun-18 3:11am    
5ed!
Agarwal1984 12-Jun-18 6:34am    
my priority using c# code,and i want to format a particular column. Its a client need not mine.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900