开发者

reading excel file -> getting checkbox value

开发者 https://www.devze.com 2023-04-11 09:11 出处:网络
So i think ive tried everything now. Im trying to get the values from radiobuttons and checkboxes from an excel sheet. My first approach was to use the Excel Data Reader: http://exceldatareader.codepl

So i think ive tried everything now. Im trying to get the values from radiobuttons and checkboxes from an excel sheet. My first approach was to use the Excel Data Reader: http://exceldatareader.codeplex.com/. The cells with checkboxes render empty.

Same thing if i use OLEDB;

string filename = @"C:\\" + "uploads\\SmartAuditSheet.xls";
        string connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" +
                                      "Data Source=" + filename + ";" +
                            开发者_StackOverflow          "Extended Properties=Excel 8.0;";

        OleDbDataAdapter dataAdapter = new OleDbDataAdapter("SELECT * FROM [Sheet1$]",    connectionString);

        DataSet myDataSet = new DataSet();
        dataAdapter.Fill(myDataSet, "BookInfo");
        DataTable dataTable = myDataSet.Tables["BookInfo"];

        gv.DataSource = myDataSet;
        gv.DataBind();

Help please.


I would suggest you try something like the following.

 OLEObject ole = (OLEObject)excelWorksheet.OLEObjects("Checkbox1"); 


I would recommend using some 3rd-party library for that - there are several out there (free and commercial) that do NOT require Excel being installed:

  • OpenXML 2.0 (free library from MS) can be used to read/modify the content of an .xlsx so you can do with it what you want

  • EPPlus (free library) works with XLSX

  • some (commercial) 3rd-party libraries come with grid controls allowing you to do much more with excel files (most can do not only XLSX but XLS too) in your application (be it Winforms/WPF/ASP.NET...) like SpreadsheetGear, Aspose.Cells, Flexcel etc.


bool state = Convert.ToBoolean(ws.OLEObjects("Checkbox1").Object.value());


You get the object values using OpenXML. Below Code shows how to get checkbox object values using OpenXML.

using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Spreadsheet;
public static bool GetCheckBoxValue( String filePath )
{
    bool isChecked = false;
    using (SpreadsheetDocument document = SpreadsheetDocument.Open(filePath, false))                   
    {
        WorkbookPart wbPart = document.WorkbookPart;
        // Sheet object to retrieve a reference to the first worksheet.
        Sheet theSheet = wbPart.Workbook.Descendants<Sheet>().Where(s => s.Name == "Sheet1").FirstOrDefault();
        var control = wsPart.Worksheet.Descendants<DocumentFormat.OpenXml.Spreadsheet.Control>().FirstOrDefault();
        var controlProperies = (ControlPropertiesPart)wsPart.GetPartById(control.Id);
        isChecked = controlProperies.FormControlProperties.Checked == "Checked";
    }
    return isChecked ;
}
0

精彩评论

暂无评论...
验证码 换一张
取 消