一键使用C#将数据导出到Excel [英] Export Data to Excel Using C# in One Colume

查看:120
本文介绍了一键使用C#将数据导出到Excel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好frds,

我想将表格数据导出到excel工作表中.但我想在Excel的一个单元格中需要该数据.我不明白.
我有包含4个字段的表.但是我想在一个Excel单元格中获得4个字段数据.....

hello frds,

i want to export my table data in excel sheet .i do that and it work. but i want to need that data in one cell of excel. that i don''t get.
i have table which have contain 4 field. but i want to get that 4 field data in one cell of excel.....

推荐答案

以下是我用来将数据导出到excel的一些代码:

The following is some code that I have used to export data to excel:

private static void Excel(string fileName, List<IDirectoryInventoryDataCollector> list)
{
    try
    {
        var xlApp = new Excel.Application();
        var xlWorkBook = xlApp.Workbooks.Add();
        var xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
        ExcelTitleRow(list[0], 1, xlWorkSheet);

        int row = 2;
        foreach (var item in list)
        {
            ExcelFillRow(item, row++, xlWorkSheet);
        }

        for (int i = 1; i < list[0].MaxLevel - 1; i++)
        {
            ((Range)xlWorkSheet.Columns[i]).ColumnWidth = 2;
        }
        ((Range)xlWorkSheet.Columns[list[0].MaxLevel - 1]).ColumnWidth = 30;
        ((Range)xlWorkSheet.Rows[1]).WrapText = true;
        ((Range)xlWorkSheet.Rows[1]).HorizontalAlignment = HorizontalAlignment.Center;
        ((Range)xlWorkSheet.Cells[1, 1]).WrapText = false;

        xlWorkBook.SaveAs(fileName);
        xlWorkBook.Close();
        xlApp.Quit();
    }
    catch (AccessViolationException)
    {
        System.Windows.Forms.MessageBox.Show(
             "Have encountered access violation. This could be issue with Excel 2000 if that is only version installed on computer",
             "Access Violation");
    }
    catch (Exception)
    {
        System.Windows.Forms.MessageBox.Show("Unknown error",
             "Unknown error");
    }
}

private static void ExcelFillRow(IDirectoryInventoryDataCollector item, int row, Excel.Worksheet sheet)
{
    sheet.Cells[row, item.Level] = item.Name;
    int column = item.MaxLevel;
    foreach (var property in item.GetProperties())
    {
        sheet.Cells[row, column++] = property;
    }
}

private static void ExcelTitleRow(IDirectoryInventoryDataCollector item, int row, Excel.Worksheet sheet)
{
    sheet.Cells[row, 1] = "Name";
    int column = item.MaxLevel;
    foreach (var property in item.GetPropertyNames())
    {
        sheet.Cells[row, column++] = property;
    }
}



必须根据您的需要对其进行适当的修改.在插入Excel单元格之前,您应该能够在字段之间加上制表符,逗号或其他内容.



It will have to be appropriately modified for what you need. You should be able to put tabs, commas, or something else between the fields before inserting into an Excel cell.


在没有看到您已完成的代码的情况下,很难说问题是什么,但是如果您现在可以将数据添加到四个单元格中,然后将四个值连接起来,然后将结果添加到单个单元格中.
Without seeing the code you have done, it''s quite hard to say what the problem is, but if you can now add data to four cells, then concatenate the four values and then add the result into a single cell.


尝试一下
如何通过使用Visual C#2005或Visual C#.NET [ CopyFromRecordset方法 [ ^ ]
Try this
How to transfer data to an Excel workbook by using Visual C# 2005 or Visual C# .NET[^]
or this:
CopyFromRecordset Method[^]


这篇关于一键使用C#将数据导出到Excel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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