读取excel文件,根据条件以c#窗体形式打印

时间:2015-08-04 09:58:15

标签: c# excel printing

如果我的excel文件中有以下数据,请注意它可能有也可能没有来自A-Z列的数据

  A     B      C
1 1     97     testing
2 5     102    test
3 8     42     tessst

在c#窗体上有一个测试框,提示用户输入行号,如果他们输入行号1,那么它必须只带回那一行。

我的尝试

private void btnGetRow_Click(object sender, EventArgs e)
{
    var RowNumber =  txtRowNumber.Text;
    var path = "C:\\Book1.xlsx;";          

    if (txtRowNumber.Text == string.Empty || Convert.ToInt32(RowNumber) == 0)
    {
        MessageBox.Show("Please enter a digit > 0 ");
    }
    else
    {
        string con = " Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties=Excel 12.0;";
        using (OleDbConnection connection = new OleDbConnection(con))
        {
            connection.Open();
            OleDbCommand command = new OleDbCommand("select * from [Product$] where F1=Rownumber ", connection); // hear it must filter from the textbox,which it doesnt do so,it gives an error 
           using (OleDbDataReader dr = command.ExecuteReader())
            {
                while (dr.Read())
               {
                  //here im not sure how to get the columns from A-Z
                  //send data to the printer
                }
            }

        }

        Console.WriteLine(RowNumber);
    }
}

1 个答案:

答案 0 :(得分:0)

请考虑使用OpenXML SDK。通过这种访问XLSX表格非常简单: 使用这个简单的Code Snipped,您可以获得Spreadsheets Row并获取它的单元格值:

    private void GetValues()
    {
        OpenFileDialog fd = new OpenFileDialog();
        fd.ShowDialog();
        var x = fd.FileName;

        using (SpreadsheetDocument myDoc = SpreadsheetDocument.Open(x, true))
        {
            WorkbookPart workbookPart = myDoc.WorkbookPart;
            WorksheetPart worksheetPart = workbookPart.WorksheetParts.First();
            SheetData sheetData =
            worksheetPart.Worksheet.Elements<SheetData>().First();
            var row = sheetData.Elements<Row>().ToArray()[1];

            foreach (Cell c in row.Elements<Cell>())
            {
               MessageBox.Show(c.CellValue.Text);
            }
        }
    }

修改:如果您的单元格包含非数字值,则必须从SharedStringTable获取值,并查看this答案。 那么你的代码必须如下所示:

    private void GetNonNumericValues()
    {
        OpenFileDialog fd = new OpenFileDialog();
        fd.ShowDialog();
        var x = fd.FileName;

        using (SpreadsheetDocument myDoc = SpreadsheetDocument.Open(x, true))
        {
            SharedStringTable sharedStringTable = myDoc.WorkbookPart.SharedStringTablePart.SharedStringTable;
            WorkbookPart workbookPart = myDoc.WorkbookPart;
            WorksheetPart worksheetPart = workbookPart.WorksheetParts.First();
            SheetData sheetData =
            worksheetPart.Worksheet.Elements<SheetData>().First();
            var row = sheetData.Elements<Row>().ToArray()[0];

            foreach (Cell c in row.Elements<Cell>())
            {
                if (c.DataType == CellValues.SharedString)
                {
                    var cellValue = c.InnerText;
                    MessageBox.Show(sharedStringTable.ElementAt(Int32.Parse(cellValue)).InnerText);
                }
            }
        }
    }