如何写EXCEL的新表数据 [英] How to write data on new sheet of EXCEL

查看:112
本文介绍了如何写EXCEL的新表数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有mysql程序将返回5桌,现在我需要设置这些表单Excel文件5个不同的表。
我使用VS 2010,Jquery的,ASP.Net。
如何编写表格的Excel中一个新的表文件。

I have Mysql procedure which will return 5 tables, Now i need to set those tables to Single Excel files 5 different sheets. I am using VS 2010, Jquery, ASP.Net. How to write that table's in excel file on a new sheet.

    $("#btnExcel").click(function (e) {


$('#divExcelExporting').html($('#containerOne').html());
$('#divExcelExporting').append($('#containerTwo').html());
$('#divExcelExporting').append($('#containerThree').html());
$('#divExcelExporting').append($('#containerFour').html());
$('#divExcelExporting').append($('#containerFive').html());

var trcoll = $('#divExcelExporting').find('.border-middle1').find('tr');

$.each(trcoll, function (d, f) {
    $($(this).find('td')[0]).remove();
});

var trcol2 = $('#divExcelExporting').find('.border-middle2').find('tr');

$.each(trcol2, function (d, f) {
    $($(this).find('td')[0]).remove();
});

var trcol3 = $('#divExcelExporting').find('.border-middle3').find('tr');

$.each(trcol3, function (d, f) {
    $($(this).find('td')[0]).remove();
});

var trcol4 = $('#divExcelExporting').find('.border-middle4').find('tr');

$.each(trcol4, function (d, f) {
    $($(this).find('td')[0]).remove();
});

var trcol5 = $('#divExcelExporting').find('.border-middle5').find('tr');

$.each(trcol5, function (d, f) {
    $($(this).find('td')[0]).remove();
});


WebService.SetVendorHTML($('#divExcelExporting').html(), OnWSRequestComplete);

});

推荐答案

我的解决方案从OpenXML的。

I got solution from OpenXML.

     string path = Context.Server.MapPath("~/ExcelData/test.xslx");
     ExportDataSet(ds, path);


  private void ExportDataSet(DataSet ds, string destination)
{
    using (var workbook = SpreadsheetDocument.Create(destination, DocumentFormat.OpenXml.SpreadsheetDocumentType.Workbook))
    {
        var workbookPart = workbook.AddWorkbookPart();

        workbook.WorkbookPart.Workbook = new DocumentFormat.OpenXml.Spreadsheet.Workbook();

        workbook.WorkbookPart.Workbook.Sheets = new DocumentFormat.OpenXml.Spreadsheet.Sheets();

        foreach (System.Data.DataTable table in ds.Tables)
        {

            var sheetPart = workbook.WorkbookPart.AddNewPart<WorksheetPart>();
            var sheetData = new DocumentFormat.OpenXml.Spreadsheet.SheetData();
            sheetPart.Worksheet = new DocumentFormat.OpenXml.Spreadsheet.Worksheet(sheetData);

            DocumentFormat.OpenXml.Spreadsheet.Sheets sheets = workbook.WorkbookPart.Workbook.GetFirstChild<DocumentFormat.OpenXml.Spreadsheet.Sheets>();
            string relationshipId = workbook.WorkbookPart.GetIdOfPart(sheetPart);

            uint sheetId = 1;
            if (sheets.Elements<DocumentFormat.OpenXml.Spreadsheet.Sheet>().Count() > 0)
            {
                sheetId =
                    sheets.Elements<DocumentFormat.OpenXml.Spreadsheet.Sheet>().Select(s => s.SheetId.Value).Max() + 1;
            }

            DocumentFormat.OpenXml.Spreadsheet.Sheet sheet = new DocumentFormat.OpenXml.Spreadsheet.Sheet() { Id = relationshipId, SheetId = sheetId, Name = table.TableName };
            sheets.Append(sheet);

            DocumentFormat.OpenXml.Spreadsheet.Row headerRow = new DocumentFormat.OpenXml.Spreadsheet.Row();

            List<String> columns = new List<string>();
            foreach (System.Data.DataColumn column in table.Columns)
            {
                columns.Add(column.ColumnName);

                DocumentFormat.OpenXml.Spreadsheet.Cell cell = new DocumentFormat.OpenXml.Spreadsheet.Cell();
                cell.DataType = DocumentFormat.OpenXml.Spreadsheet.CellValues.String;
                cell.CellValue = new DocumentFormat.OpenXml.Spreadsheet.CellValue(column.ColumnName);
                headerRow.AppendChild(cell);
            }


            sheetData.AppendChild(headerRow);

            foreach (System.Data.DataRow dsrow in table.Rows)
            {
                DocumentFormat.OpenXml.Spreadsheet.Row newRow = new DocumentFormat.OpenXml.Spreadsheet.Row();
                foreach (String col in columns)
                {
                    DocumentFormat.OpenXml.Spreadsheet.Cell cell = new DocumentFormat.OpenXml.Spreadsheet.Cell();
                    cell.DataType = DocumentFormat.OpenXml.Spreadsheet.CellValues.String;
                    cell.CellValue = new DocumentFormat.OpenXml.Spreadsheet.CellValue(dsrow[col].ToString()); //
                    newRow.AppendChild(cell);
                }

                sheetData.AppendChild(newRow);
            }

        }
    }
}

参考文献:

using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Spreadsheet;
using DocumentFormat.OpenXml;

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

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