简体   繁体   English

默认情况下,如何生成类型为“文本”而不​​是“常规”的Excel文件

[英]How do I generate Excel file with type 'Text' not 'General' by default

I'm working on c# project where I'm generating dynamic excel file from my gridview. 我正在研究c#项目,我正在从gridview生成动态excel文件。

when I successfully generate the file it is converting all data into Genaral type but I need that all excel file data into Text format, 当我成功生成文件时,它将所有数据转换为Genaral类型,但我需要将所有excel文件数据转换为Text格式,

How can I do that? 我怎样才能做到这一点? As I need to use this file further for reporting purpose and need Text as type 因为我需要进一步使用此文件用于报告目的并需要Text作为类型

Following is my excel file generation c# code : 以下是我的excel文件生成c#代码:

private void ExportGridView()
        {
            System.IO.StringWriter sw = new System.IO.StringWriter();
            System.Web.UI.HtmlTextWriter htw = new System.Web.UI.HtmlTextWriter(sw);

            // Render grid view control.
            gvFiles.RenderControl(htw);

            // Write the rendered content to a file.
            string renderedGridView = sw.ToString();
            File.WriteAllText(@"C:\test\ExportedFile.xls", renderedGridView);
        }

在此输入图像描述

I think I need to use Microsoft.Office.Interop.Excel and I can specify Type also like to generate in Text 我想我需要使用Microsoft.Office.Interop.Excel ,我可以指定Type也喜欢在Text生成

Also I searched like we can generate it as a like html contain and make it excel something like that. 我也搜索,就像我们可以生成它像一个像html包含,并使其优秀的东西。

can anyone help me? 谁能帮我?

You can use Microsoft.Office.Interop.Excel . 您可以使用Microsoft.Office.Interop.Excel Add a reference to it and add the following using statement beside the other using statements. 添加一个引用到它,并添加以下using其他using语句旁的语句。

using Microsoft.Office.Interop.Excel;

Modify the ExportGridView method: 修改ExportGridView方法:

private void ExportGridView()
{
    var path = @"C:\test\ExportedFile.xls";
    System.IO.StringWriter sw = new System.IO.StringWriter();
    System.Web.UI.HtmlTextWriter htw = new System.Web.UI.HtmlTextWriter(sw);

    // Render grid view control.
    gvFiles.RenderControl(htw);

    // Write the rendered content to a file.
    string renderedGridView = sw.ToString();
    File.WriteAllText(path, renderedGridView);
    UpdateFormat(path);
}

Add the following method: 添加以下方法:

private void UpdateFormat(string path)
{
    var application = new Microsoft.Office.Interop.Excel.Application();
    var doc = application.Workbooks.Open(path);
    for (int i = 1; i <= doc.Worksheets.Count; i++)
    {
        Worksheet sheet = doc.Worksheets[i];
        for (int j = 1; j <= sheet.Columns.Count; j++)
        {
            Range column = sheet.Columns[j];
            column.NumberFormat = "@";
        }
    }
    doc.Save();
    doc.Close();
    application.Quit();
}

Just a guess, but its because you're writing the gridview html? 只是一个猜测,但它是因为你正在编写gridview html? You can just create a CSV file and save it with .xls and it'll be an excel document 你可以创建一个CSV文件并用.xls保存它,它将是一个excel文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM