直接转换成锯齿状数组二维数组不反复的每个项目? [英] Convert a jagged array to a 2D array directly without iterating each item?

查看:277
本文介绍了直接转换成锯齿状数组二维数组不反复的每个项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想一个DataTable保存到Excel表格中..我的代码是这样的..

I am trying to save a DataTable into an excel sheet.. my code is like this..

Excel.Range range = xlWorkSheet.get_Range("A2");
range = range.get_Resize(dtExcel.Rows.Count, dtExcel.Columns.Count);
object[,] rng1 = new object[dtExcel.Rows.Count, dtExcel.Columns.Count];



Excel范围要求的范围值数组[,],但我有数据表作为交错数组[] [ ]

Excel range requires range value as array[,] but I have the DataTable as jagged array[][].

object[][] rng2 = dtExcel.AsEnumerable().Select(x => x.ItemArray).ToArray();



有没有内置的功能交错数组[] []直接转换为二维数组[] []?通过Excel的
迭代,数据表和分配似乎与大量的数据慢..

Is there any built-in function to directly convert the jagged array[][] to a 2D array[][] ? Iterating through Excel, DataTable and assigning seems slower with bulk data..

此外,我不希望与设置DSN为Excel查询..我选择Excel中存储,以避免任何数据库的配置...:P
我发现写数据的方法来这里..擅长
http://support.microsoft.com/kb/306023

Also I don't want to setup querying with DSN for excel.. I chose excel storage to avoid the configuring of any databases.. :P I found a detailed explanation of ways of writing data to excel here.. http://support.microsoft.com/kb/306023

推荐答案

最后我用 NPOI库这一点。这是很简单,并且免费。代码转换DataTable中脱颖而出如下。

At last I used NPOI library for this. It is quite simple and free. the code to convert DataTable to excel as follows.

HSSFWorkbook hssfworkbook = new HSSFWorkbook();
        foreach (DataTable dt in DataSource.Tables)
        {
            ISheet sheet1 = hssfworkbook.CreateSheet(dt.TableName);

            //Set column titles
            IRow headRow = sheet1.CreateRow(0); 
            for (int colNum = 0; colNum < dt.Columns.Count; colNum++)
            {
                ICell cell = headRow.CreateCell(colNum);
                cell.SetCellValue(dt.Columns[colNum].ColumnName);
            }

            //Set values in cells
            for (int rowNum = 1; rowNum <= dt.Rows.Count; rowNum++)
            {
                IRow row = sheet1.CreateRow(rowNum);
                for (int colNum = 0; colNum < dt.Columns.Count; colNum++)
                {
                    ICell cell = row.CreateCell(colNum);
                    cell.SetCellValue(dt.Rows[rowNum - 1][colNum].ToString());
                }
            }

            // Resize column width to show all data
            for (int colNum = 0; colNum < dt.Columns.Count; colNum++)
            {
                sheet1.AutoSizeColumn(colNum);
            }
        }

这篇关于直接转换成锯齿状数组二维数组不反复的每个项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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