c#导出execl的单元格为文本格式

文本:@
日期:yyyy/mm/dd
数字:#,##0.00
货币:¥#,##0.00
百分比:#0.00%

 public static MemoryStream ExportToExcelText(DataTable table)
        {
            MemoryStream ms = new MemoryStream();
            IWorkbook workbook = new HSSFWorkbook();
            IDataFormat dataformat = workbook.CreateDataFormat();
            ICellStyle style0 = workbook.CreateCellStyle();
            style0.DataFormat = dataformat.GetFormat("@");//文本格式
            int rowIndex = 1;
            ISheet sheet = null;
            int n = 1;
            int count = table.Rows.Count;
            if (count == 0)
            {//没有数据导出表头
                sheet = workbook.CreateSheet("Sheet1");//创建sheet页
                IRow headerRow = sheet.CreateRow(0);
                foreach (DataColumn column in table.Columns)//表头
                {
                    headerRow.CreateCell(column.Ordinal).SetCellValue(column.Caption);//If Caption not set, returns the ColumnName value
                }
            }
            else
            {
                foreach (DataRow row in table.Rows)//循环数据
                {
                    if (rowIndex == 1 || rowIndex % 60000 == 0)
                    {
                        sheet = workbook.CreateSheet("Sheet" + n);//创建sheet页
                        IRow headerRow = sheet.CreateRow(0);
                        foreach (DataColumn column in table.Columns)//表头
                        {
                            headerRow.CreateCell(column.Ordinal).SetCellValue(column.Caption);//If Caption not set, returns the ColumnName value
                        }
                        n++;
                        rowIndex = 1;
                    }
                    IRow dataRow = sheet.CreateRow(rowIndex);//行
                    foreach (DataColumn column in table.Columns)
                    {
                        dataRow.CreateCell(column.Ordinal).SetCellValue(row[column].ToString());
                        dataRow.GetCell(column.Ordinal).CellStyle = style0;//每行单元格格式都为文本格式
                    }
                    rowIndex++;
                }
            }
            workbook.Write(ms);
            ms.Flush();
            ms.Position = 0;
            return ms;
        }

 

C# 中,你可以使用 EPPlus 这个开源库来方便地操作 Excel 文件,包括将数据导出为纯文本格式。EPPlus 提供了对 Microsoft Office Open XML 格式的支持,这使得处理 Excel 数据变得更加简单。 以下是使用 EPPlus 导出 Excel 表格所有列为文本格式的基本步骤: 1. 安装 EPPlus NuGet 包:通过 Visual Studio 的包管理器安装 "EPPlus",`Install-Package EPPlus`。 2. 引入库:在你的 C# 项目中引用 `using OfficeOpenXml;` 3. 创建 Excel 工作簿和工作表: ```csharp using (ExcelPackage excel = new ExcelPackage()) { ExcelWorksheet worksheet = excel.Workbook.Worksheets.Add("Sheet1"); } ``` 4. 添加数据到工作表,然后设置单元格格式文本: ```csharp var data = ... // 你的数据源 foreach (var item in data) { int rowIndex = worksheet.Dimension.End.Row; worksheet.Cells[rowIndex, 1].Value = item.Field1; worksheet.Cells[rowIndex, 2].Value = item.Field2; // 对每一列做同样的操作,将字段值赋给对应单元格,并设置文本格式 worksheet.Cells[rowIndex, columnIndex].Style.Numberformat.Format = @"0"; } ``` 5. 设置文本格式: ```csharp worksheet.Cells[StartRow, EndColumn].Style.Numberformat.Format = @"General"; // 将所有单元格设为文本格式 ``` 6. 保存文件: ```csharp excel.SaveAs(new FileInfo(@"C:\output\textfile.xlsx")); // 替换为你需要保存的位置 ``` 记得在实际应用中处理异常,例如检查文件是否成功保存等。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值