简体   繁体   English

从.xlsx文件中读取数据时

[英]While Reading data from a .xlsx file

string Code = "";
  if (fileUp.HasFile)
  {
    string Path = fileUp.PostedFile.FileName;
// initialize the Excel Application class
    ApplicationClass app = new ApplicationClass();

// create the workbook object by opening  the excel file.
    Workbook workBook = app.Workbooks.Open(Path, 0, true, 5, "", "", true,
                   XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);

// Get The Active Worksheet Using Sheet Name Or Active Sheet
    Worksheet workSheet = (Worksheet)workBook.ActiveSheet;
    int index = 0;

// This row,column index should be changed as per your need.
// that is which cell in the excel you are interesting to read.
    object rowIndex = 2;
    object colIndex1 = 1;
    object colIndex2 = 2;
    object colIndex3 = 3;
    object colIndex4 = 4;
    object colIndex5 = 5;
    object colIndex6 = 6;
    object colIndex7 = 7;
    try
    {
      while (((Range)workSheet.Cells[rowIndex, colIndex1]).Value2 != null)
      {
        rowIndex = 2 + index;

//string QuestionCode = (index + 1).ToString();
        string QuestionCode = ((Range)workSheet.Cells[rowIndex, colIndex1]).Value2.ToString();
        string QuestionText = ((Range)workSheet.Cells[rowIndex, colIndex2]).Value2.ToString();
        string CorrectAnswer = ((Range)workSheet.Cells[rowIndex, colIndex3]).Value2.ToString();
        string ChoiceA = ((Range)workSheet.Cells[rowIndex, colIndex4]).Value2.ToString();
        string ChoiceB = ((Range)workSheet.Cells[rowIndex, colIndex5]).Value2.ToString();
        string ChoiceC = ((Range)workSheet.Cells[rowIndex, colIndex6]).Value2.ToString();
        string ChoiceD = ((Range)workSheet.Cells[rowIndex, colIndex7]).Value2.ToString();
// string ChoiceE = ((Excel.Range)workSheet.Cells[rowIndex, colIndex7]).Value2.ToString();

        newQuestionElement = new XElement("Question");
        XElement optionElement = new XElement(QuestionElement.Option);
        questionType = ddlQusType.SelectedValue.ToByte();
        if (!string.IsNullOrEmpty(QuestionText))
          newQuestionElement.Add(new XElement(QuestionElement.QuestionText, QuestionText));
        else
        {

//lblMessage.Text = "Missing question in Qus No.: " + i;
          break;
        }
        newQuestionElement.Add(new XElement(QuestionElement.QuestionType, questionType));

//newQuestionElement.Add(new XElement(QuestionElement.Randomize, chbRandomizeChoice.Checked));
        newQuestionElement.Add(new XElement(QuestionElement.Answer, CorrectAnswer));
        if (ChoiceA.Trim() != string.Empty)
           optionElement.Add(new XElement("A", ChoiceA));
        if (ChoiceB.Trim() != string.Empty)
           optionElement.Add(new XElement("B", ChoiceB));
        if (ChoiceC.Trim() != string.Empty)
           optionElement.Add(new XElement("C", ChoiceC));
        if (ChoiceD.Trim() != string.Empty)
           optionElement.Add(new XElement("D", ChoiceD));
        newQuestionElement.Add(optionElement);                        

        index++;
        saveData(QuestionCode.ToString());

I am using this code to retrieve the data from .xlsx file. 我正在使用此代码从.xlsx文件中检索数据。

But if the file has any special characters in it, it is showing it as different, like so 但是如果文件中有任何特殊字符,它会显示为不同,就像这样

The set S = {1,2,33……….12} is to be partitioned into three sets
A,B,C of equal size. Thus,      `A U B U C = S,` 

The set S = {1,2,33……….12} is to be partitioned into three sets
A,B,C of equal size. Thus,      `A È B È C = S,` 

Looks like an encoding issue. 看起来像编码问题。

I use to have this issue after reading Excel into a data table and then serializing the data table to a file. 我在将Excel读入数据表然后将数据表序列化为文件后使用此问题。

Every time I would read the data back in from the serialized file, some symbols would be replaced with funny A's and E's. 每次我从序列化文件中读回数据时,一些符号将被有趣的A和E替换。

I discovered the problem was with the encoding I was using. 我发现问题出在我使用的编码上。 I then started to store excel data using Unicode encoding and have never encounter another symbol problem with Excel data again. 然后我开始使用Unicode编码存储excel数据,并且再也没有遇到过Excel数据的另一个符号问题。

I hope this helps... 我希望这有帮助...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM