Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/32.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
Asp.net 基于分号使用C将csv转换为xls_Asp.net - Fatal编程技术网

Asp.net 基于分号使用C将csv转换为xls

Asp.net 基于分号使用C将csv转换为xls,asp.net,Asp.net,在这段代码中,我得到了一些错误,即类工厂不可用。该程序是关于在分号的基础上转换csvcomma分隔值文件int xlsMicrosoft Excel文件的。有人能帮我吗 using Microsoft.Office.Interop.Excel; using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using Sys

在这段代码中,我得到了一些错误,即类工厂不可用。该程序是关于在分号的基础上转换csvcomma分隔值文件int xlsMicrosoft Excel文件的。有人能帮我吗

using Microsoft.Office.Interop.Excel;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Excel = Microsoft.Office.Interop.Excel;
using Microsoft.Office.Core;
using Microsoft.Office.Tools.Excel;
namespace ReadWriteCSV
{
    public partial class Form1 : Form
    {

        public Form1()
        {
            InitializeComponent();

        }

        private void button1_Click(object sender, EventArgs e)
        {
            Excel.Application excel = new Excel.Application();
            Excel.Workbook workBook = excel.Workbooks.Add();
            Excel.Worksheet sheet = workBook.ActiveSheet;

            string fullPath = @"D:\Work\Sep-14\ReadWriteCSV\ReadWriteCSV\File\diff_16122014095440.csv";
            string[] fileRows = File.ReadAllLines(fullPath, Encoding.UTF8);

            foreach (string rows in fileRows)
            {
                var columns = rows.Split(';');

                for (int j = 0; j < fileRows.Length; j++)
                {
                    for (int i = 0; i < columns.Length; i++)
                    {
                        List<string> elements = new List<string>();
                        foreach (string col in columns)
                        {
                            elements.Add(col);
                            sheet.Cells[i + 1, j + 1] = col;
                        }
                        }
                    }
                }
            workBook.SaveAs(@"D:\Work\Sep-14\ReadWriteCSV\ReadWriteCSV\File\WriteXls.xlsx");
            workBook.Close();
            }
        }
    }

使用Excel打开csv文件并将其另存为xlsx

您的问题如下:

Excel索引列使用字母而不是数字,如A、B、C、D、E、F、G、H等

您需要将数字索引转换为字母索引。如果列数不超过26列,则可以使用ASCII作为偏移量

sheet.Cells[i + 1, ((char)(j+ 65)).ToString()] = col; //65 is ASCII for letter A
编辑:

您的代码还有一个问题。为什么要在内部循环中再次遍历列?与外部foreach循环相同。您需要的所有代码如下:

for (int i = 0; i< fileRows.length;i++)
{
   string row = fileRows[i];
   string[] columns = row.Split(';');
   for (int j =0; j<columns.length;j++)
      sheet.Cells[i + 1, ((char)(j+ 65)).ToString()] = columns[j];
}

就这样。您不需要编写内部循环,因为它将花费大量无用的处理。

我也做了同样的事情。请建议其他解决方案或查找上述程序中的错误