Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/lua/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
通过OpenXML将数据表转换为excel_Excel_Datatable_Openxml - Fatal编程技术网

通过OpenXML将数据表转换为excel

通过OpenXML将数据表转换为excel,excel,datatable,openxml,Excel,Datatable,Openxml,我有一个这样的数据表 Col1 Col2 Col3 col4 GRPs 2009 69952.4 a GRPs 2010 58949.8 a GRPs 2009 37251.2 b GRPs 2010 35433.9 b GRPs 2009 28039.2 c GRPs 2010 35079.4 c SOC 2009 69952.4

我有一个这样的数据表

Col1 Col2 Col3 col4 GRPs 2009 69952.4 a GRPs 2010 58949.8 a GRPs 2009 37251.2 b GRPs 2010 35433.9 b GRPs 2009 28039.2 c GRPs 2010 35079.4 c SOC 2009 69952.4 a SOC 2010 58949.8 a SOC 2009 37251.2 b SOC 2010 35433.9 b SOC 2009 28039.2 c SOC 2010 35079.4 c Col1 Col2 Col3 col4 GRPs 2009 69952.4 a GRPs 2010 58949.8 a GRPs 2009 37251.2 b GRPs 2010 35433.9 b GRPs 2009 28039.2 c GRPs 2010 35079.4 c SOC 2009 69952.4 a SOC 2010 58949.8 a SOC 2009 37251.2 b SOC 2010 35433.9 b SOC 2009 28039.2 c SOC 2010 35079.4 c 我需要一个文件来使用这种格式的OpenXML将其“转换”为excel文件

A B C D E F G H I 1 2009 2010 2009 2010 2009 2010 2 GRPs 69952.4 58949.8 37251.2 35433.9 28039.2 35079.4 3 SOC 69952.4 58949.8 37251.2 35433.9 28039.2 35079.4 A B C D E F G H I 1 2009 2010 2009 2010 2009 2010 2 GRPs 69952.4 58949.8 37251.2 35433.9 28039.2 35079.4 3 SOC 69952.4 58949.8 37251.2 35433.9 28039.2 35079.4
提前感谢

它有点像这样(抱歉,这不是完整的代码,但应该会有帮助):

//列标题
静态字符串[]headerColumns=新字符串[]{“A”、“B”、“C”};
//用于标题列计数
静态字符串字母表=“abcdefghijklmnopqrstuvxyz”;
foreach(字母表中的字符c.Split(字母表[dt.Columns.Count])[0])
标题列[字母表索引(c)]=c.ToString();
//循环遍历数据表并将数据行追加到图纸数据上
for(int r=0;r
亲爱的朋友谢谢你的帮助!我不能发布图片,所以我“画”了EXCEL表格,这就是为什么在第二个模式中有大写字母。。。我真的不需要他们在代码中。
//Column headers
static string[] headerColumns = new string[] { "A", "B", "C" };

//Used for header column counting
static string alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

         foreach (char c in alphabet.Split(alphabet[dt.Columns.Count])[0])
                headerColumns[alphabet.IndexOf(c)] = c.ToString();

            //Loops through the data table and appends the data rows onto sheet data
            for (int r = 0; r < dt.Rows.Count; r++)
            {
                //Builds a list of values from the data row
                List<string> values = new List<string>();
                for (int i = 0; i < dt.Columns.Count; i++)
                    values.Add(dt.Rows[r][i].ToString());

                //Creates a spreadsheet row from the list of values
                Spreadsheet.Row contentRow = CreateContentRow(values, r + 2);

                //Appends the row to the sheetData
                sheetData.AppendChild(contentRow);
            }

    private static Spreadsheet.Row CreateContentRow(List<string> values, int index)
    {
        //Create the new row.
        Spreadsheet.Row r = new Spreadsheet.Row();
        r.RowIndex = (UInt32)index;

        //Create the cells that contain the data.
        for (int i = 0; i < headerColumns.Length; i++)
        {
            Spreadsheet.Cell c = new Spreadsheet.Cell();
            c.CellReference = headerColumns[i] + index;
            Spreadsheet.CellValue v = new Spreadsheet.CellValue();
            v.Text = values[i];
            c.AppendChild(v);
            r.AppendChild(c);
        }
        return r;
    }