本文介绍了转换成CSV XLS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在块分开的Web应用程序工作,我越来越远离我,我必须转换成XLS要传递到一个Excel处理器,他们建立的工作伙伴一个CSV对象。
此CSV对象由字符;分隔

I'm working in a web application separated in blocks and I'm getting a CSV object from a work mate of mine which I must convert into XLS to be passed into an Excel Processor they built.This CSV object is delimited by the character ";".

我想知道的是,我怎么能在CSV对象转换为XLS编程。

What I'd like to know is how I can convert the CSV object into XLS programatically.

推荐答案

这应该很容易让你的CSV对象转换成字符串数组的数组,然后做如以下例如,(你需要添加到一个的Microsoft.Office.Interop.Excel引用):

It should be easy for you to convert the CSV object into an array of arrays of strings and then do like in the following example (you'll need to add a reference to Microsoft.Office.Interop.Excel):

using Excel = Microsoft.Office.Interop.Excel;

Excel.Application excel = new Excel.Application();
Excel.Workbook workBook = excel.Workbooks.Add();
Excel.Worksheet sheet = workBook.ActiveSheet;

var CsvContent = new string[][]
{
    new string[] {"FirstName", "UserName", "PostCode", "City"},
    new string[] {"John", "Smith", "4568", "London"},
    new string[] {"Brian", "May", "9999", "Acapulco"}
};

for (int i = 0; i < CsvContent.Length; i++)
{
    string[] CsvLine = CsvContent[i];
    for (int j = 0; j < CsvLine.Length; j++)
    {
        sheet.Cells[i + 1, j + 1] = CsvLine[j];
    }
}

workBook.SaveAs(@"C:\Temp\fromCsv.xls");
workBook.Close();

这篇关于转换成CSV XLS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 14:38