如何将数据导出到多个工作表 [英] How to export data to multiple sheets

查看:119
本文介绍了如何将数据导出到多个工作表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hello All,



我有两个数据表,我需要导出到excel表。我必须将一个表数据发送到一个表(sheet1),另一个表发送到另一个表(sheet2)。



有可能吗?...如果是这样,请给我推荐。



提前致谢...



问候,

Naveen。

解决方案

< blockquote>是的,你可以这样做。



来自StackOverFlow。希望它有所帮助。



http://stackoverflow.com/a/13973274 [ ^ ]



  //  将DataTable导出到excel文件中,其中包含字段名称标题行 
// - 如果给出文件路径,则保存excel文件而不显示
// - 不保存excel文件,只要没有给出文件路径就可以看到它
public static void ExportToExcel( this DataTable Tbl, string ExcelFilePath = null
{
尝试
{
if (Tbl == null || Tbl.Columns.Count == 0
throw new 异常( ExportToExcel:空或空输入表!\ n );

// 加载excel,并创建新工作簿
Excel.Application excelApp = new Excel.Application();
excelApp.Workbooks.Add();

// 单个工作表
Excel._Worksheet workSheet = excelApp .ActiveSheet;

// 列标题
for int i = 0 ; i < Tbl.Columns.Count; i ++)
{
workSheet.Cells [ 1 ,(i + 1)] = Tbl.Columns [i] .ColumnName;
}

//
for int i = 0 ; i < Tbl.Rows.Count; i ++)
{
// 要执行的操作:在打印前格式化日期时间值
for int j = 0 ; j < Tbl.Columns.Count; j ++)
{
workSheet.Cells [(i + 2 ),(j + 1 )] = Tbl.Rows [i] [j];
}
}

// check fielpath
if (ExcelFilePath!= null && ExcelFilePath!=
{
尝试
{
workSheet.SaveAs(ExcelFilePath);
excelApp.Quit();
MessageBox.Show( 保存Excel文件!);
}
catch (例外情况)
{
throw new 异常( ExportToExcel:Excel文件无法保存!检查filepath.\
+ ex.Message);
}
}
其他 // 没有给出文件路径
{
excelApp.Visible = true ;
}
}
catch (例外情况)
{
throw new 例外( ExportToExcel :\ n + ex.Message);
}
}
}


Hello All,

I have two data tables which I need to export to excel sheet. I have to send one table data to one sheet (sheet1) and another to another sheet (sheet2).

Is it possible to do?... If so can you please give me the referrals.

Thanks in advance...

Regards,
Naveen.

解决方案

Yes you can do that.

From StackOverFlow. hope it helps.

http://stackoverflow.com/a/13973274[^]

// Export DataTable into an excel file with field names in the header line
    // - Save excel file without ever making it visible if filepath is given
    // - Don't save excel file, just make it visible if no filepath is given
    public static void ExportToExcel(this DataTable Tbl, string ExcelFilePath = null)
    {
        try
        {
            if (Tbl == null || Tbl.Columns.Count == 0)
                throw new Exception("ExportToExcel: Null or empty input table!\n");

            // load excel, and create a new workbook
            Excel.Application excelApp = new Excel.Application();
            excelApp.Workbooks.Add();

            // single worksheet
            Excel._Worksheet workSheet = excelApp.ActiveSheet;

            // column headings
            for (int i = 0; i < Tbl.Columns.Count; i++)
            {
                workSheet.Cells[1, (i+1)] = Tbl.Columns[i].ColumnName;
            }

            // rows
            for (int i = 0; i < Tbl.Rows.Count; i++)
            {
                // to do: format datetime values before printing
                for (int j = 0; j < Tbl.Columns.Count; j++)
                {
                    workSheet.Cells[(i + 2), (j + 1)] = Tbl.Rows[i][j];
                }
            }

            // check fielpath
            if (ExcelFilePath != null && ExcelFilePath != "")
            {
                try
                {
                    workSheet.SaveAs(ExcelFilePath);
                    excelApp.Quit();
                    MessageBox.Show("Excel file saved!");
                }
                catch (Exception ex)
                {
                    throw new Exception("ExportToExcel: Excel file could not be saved! Check filepath.\n"
                        + ex.Message);
                }
            }
            else    // no filepath is given
            {
                excelApp.Visible = true;
            }
        }
        catch(Exception ex)
        {
            throw new Exception("ExportToExcel: \n" + ex.Message);
        }
    }
}


这篇关于如何将数据导出到多个工作表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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