将excel列存储在字符串数组中(也存储空白单元格值)

时间:2018-04-19 16:35:57

标签: c# excel

我想将excel文件列存储在字符串数组中,但空白单元格值不会存储为空字符串。 例如:

excel column
C1 = "1"
C2 = "2"
C3 = blank
C4 = "3"

所以它存储为

mystring[0]="1"
mystring[1]="2"
mystring[2]="3"

省略空白值。我想把它存储为

mystring[0]="1"
mystring[1]="2"
mystring[2]=" "
mystring[3]="3"

这是我使用的代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Office.Interop.Excel;
using System.IO;

namespace ConsoleApplication6
{
    class Program
    {
        public static void Main()
        {
            string path =@"e:\contacts.xls"; 
            Application xlApp = new Application();          
            Workbook xlWb = xlApp.Workbooks.Open(path,0,true);
            Worksheet xlSheet = xlWb.Sheets[1] ; 
            Range range = xlSheet.UsedRange.Columns[2];
            System.Array myvalues = (System.Array)range.Cells.Value;
            string[] mystring = myvalues.OfType<object>().Select(o => o.ToString()).ToArray();

            foreach (var i in mystring)
                Console.WriteLine(i);
            Console.Read();
        }
    }
}

2 个答案:

答案 0 :(得分:1)

阅读总是一项有趣的任务。我通常使用的是:

object[,] matrixRead = (object[,])currentRange.Value;

空单元格显示为Null,如您所见:

enter image description here

我很确定你以后可以找到一种方法""。这是我正在使用的代码:

using System;
using Excel = Microsoft.Office.Interop.Excel;

class StartUp
{
    static void Main()
    {
        string filePath = @"E:\contacts.xls";

        int rowsCount = 5;
        int colsCount = 6;

        Excel.Application excel = new Excel.Application();
        excel.Visible = false;
        excel.EnableAnimations = false;

        Excel.Workbook wkb = Open(excel, filePath);
        Excel.Worksheet wk = (Excel.Worksheet)excel.Worksheets.get_Item(1);

        Excel.Range startCell = wk.Cells[1, 1];
        Excel.Range endCell = wk.Cells[rowsCount, colsCount];
        Excel.Range currentRange = wk.get_Range(startCell, endCell).Cells;
        currentRange.Interior.Color = Excel.XlRgbColor.rgbWhite;

        object[,] matrixRead = (object[,])currentRange.Value;
        bool[,] matrixResult = new bool[rowsCount + 1, colsCount + 1];

        excel.EnableAnimations = true;
        wkb.Close(true);
        excel.Quit();
        Console.WriteLine("Finished!");
    }

    private static Excel.Workbook Open(Excel.Application excelInstance,
            string fileName, bool readOnly = false,
            bool editable = true, bool updateLinks = true)
    {
        Excel.Workbook book = excelInstance.Workbooks.Open(
            fileName, updateLinks, readOnly,
            Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
            Type.Missing, editable, Type.Missing, Type.Missing, Type.Missing,
            Type.Missing, Type.Missing);
        return book;
    }
}

Source for the code (disclaimer - my site)

答案 1 :(得分:0)

谢谢你的Vityata
我在处理来自excel的那些空值时遇到问题 我替换了这段代码

Range range = xlSheet.UsedRange.Columns[2];
System.Array myvalues = (System.Array)range.Cells.Value;
string[] mystring = myvalues.OfType<object>().Select(o => o.ToString()).ToArray();

这一个

List<string> mystring = new List<string>();
for (int i = 1; i <= 300; ++i)            
mystring.Add(Convert.ToString((xlSheet.Cells[i, 3]).Value));

谢谢你:)