C#NPOI将单元格样式设置为“文本” /“字符串1-19”设置为日期格式/禁用任何格式 [英] C# NPOI set cell style to Text / string 1-19 is formatted as a date / disable any formating

查看:1268
本文介绍了C#NPOI将单元格样式设置为“文本” /“字符串1-19”设置为日期格式/禁用任何格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个excel,当我编写一些值示例1-19时,当我打开excel文档时,我看到1-19,但是如果我单击它,那么excel会尝试将其格式化为日期

I am creating an excel, when i write some values example 1-19, when I open the excel doc, i see 1-19, but if i click on it then excel tries to format it as a date

是否可以强制工作表不使用任何公式或格式?
我已经检查过了,数据格式是字符串。

IS THERE A WAY to force the sheet to not use any formulas or formatting? I have checked and the dataformat is string.

 private void Test1(ref ISheet worksheet, string[] array, IWorkbook workbook, int iRow, XSSFFont font2)
        {
            var format = HSSFDataFormat.GetBuiltinFormats();

            ICellStyle _TextCellStyle = workbook.CreateCellStyle();
            _TextCellStyle.DataFormat = workbook.CreateDataFormat().GetFormat("@");
            IRow file = worksheet.CreateRow(iRow);
            int iCol = 0;
            for (int y = 0; y < array.Length; y++)
            {
                ICellStyle style = workbook.CreateCellStyle();

                style.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.Grey25Percent.Index;
                //style.DataFormat = HSSFDataFormat.
                ICell cell = file.CreateCell(iCol, CellType.String);
                cell.SetCellValue(array[y]);
                style.SetFont(font2);
               // cell.CellStyle = style;
                var getst = cell.CellType;
                cell.CellStyle = _TextCellStyle;
                iCol++;
                getst = cell.CellType;

            }

        }


推荐答案

即使按照文档使用正确的格式字符串 @,您的数据仍保持常规格式。某些时间库方法在NPOI中不起作用,因此您必须尝试其他方法。

Your data remains in "General" format even after you are using correct format string "@" as per documentation. Some time library methods don't work in NPOI so you'll have to try different approach.

您可以尝试以下其中之一

You can try one of these

 _TextCellStyle.DataFormat = workbook.CreateDataFormat().GetFormat("text"); //Instead of "@"

或在为'1-19

这篇关于C#NPOI将单元格样式设置为“文本” /“字符串1-19”设置为日期格式/禁用任何格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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