I am reading Excel file using LinqToExcel:
var Excel = new ExcelQueryFactory(Filename);
Excel.databaseEngine = DatabaseEngine.Ace;
Excel.strictMapping = true;
I want to exclude certain columns, to prevent the strict mapping feature from throwing an exception for those columns.
What if you set StrictMapping to false? Will that work for you?
Related
I have a program I am writing that is to read an excel file and extract from it any column names that contain the string "Zone_" in them. This is the code I have written using C# and ClosedXML
// Read the excel file
var workbook = new XLWorkbook(file);
var worksheet = workbook.Worksheet(1);
zonesColumns = worksheet.Columns()
.Where(c => c.FirstCell().Value.ToString().StartsWith("Zone_"))
.ToList();
the problem I am running into is that instead of getting the Column name I am getting a bunch of jibberish instead. I am not sure what I did wrong within the code as to me it seems to do what I want it to. Thanks for the help!
Your query as shown is retrieving columns. A column is described as a range something like Sheet1!A1:A1048576 (is this what you mean by jibberish)?
But if I'm following, you want the names. Try getting a collection of the first cells of the retrieved columns, then select the GetText() result.
var names =
worksheet
.Columns()
.Select(col=>col.FirstCell())
.Select(cell=>cell.GetText())
.Where(name=>name.StartsWith("Zone_"))
.ToList();
I'm using NPOI in my .net core library to do some operations in my excel sheet.
I'm now want to get the used rang in the worksheet. Is there any way to achieve this target? For some reasons, I cannot use Microsoft.Office.Interop.Excel COM component.
I've solved this question by using the LastRowNum and LastCellNum property.
For example, If I have got a ISheet instance like the following:
var sheet = workbook.GetSheetAt(0);
Then, I can get the range by the LastRowNum and LastCellNum property.
var row = sheet.LastRowNum;
var column = sheet.GetRow(row).LastCellNum;
var range = new { row, column };
When adding conditional formatting to a sheet using c#
How do I set the IconSet being used?
Below isn't currently working. It gives me a set of default icons but not the ones I want.
Excel.IconSetCondition cfIconSet =
(Excel.IconSetCondition)excelWorksheet.get_Range(cellNumber, cellNumber)
.FormatConditions
.AddIconSetCondition();
cfIconSet.IconSet = Excel.XlIconSet.xl3Flags;
Solved
cfIconSet.IconSet = cfIconSet.IconSet(Excel.XlIconSet.xl3Flags);
Excel 2010.
I have a C# app that has a dataset with multiple tables. I want to export this to a workbook where each table is a separate sheet it is important to keep the order of the datasets, and the name of the data tables)
One possible solution is to loop through each table, put it on its own dataset, save this dataset as XML, then use the Application.Workbooks.OpenXML method
MSDN OpenXML Documentation
But here is the problem, if I pass the third parameter (which gives a very nice import with filters and everything), excel succeed, but it warns me that some columns were imported as text, which is ok with me (one of the columns is UPC, which should be a text, not a number).
By displaying this message it stops the process until the user clicks that this is acceptable. Then I question my self about how the mother of all excels is doing these days.
How to prevent this message from popping up?
Or another way to do this import with such nice results? (Copy and paste works but not so nicely, writing in every cell using automation is way to slow, maybe using some excel library...)
You turn
Try
var excelApplication = new Application { DisplayAlerts = false };
or
Workbook excelWorkBoook = excelApplication.Workbooks.Open(...);
excelWorkBoook.CheckCompatibility = false;
I want to allow my application to import data from XLS files. I already do this with CSV files and XML files, but would like to open the scope for users. I am having trouble with loading the file. We load the files (XLS,CSV,XML) into a data set and work on it from there. The loading code for XLS is below
FileInfo fi = new FileInfo(filename);
//create and open a connection with the supplied string
OleDbConnection objOleDBConn;
objOleDBConn = new OleDbConnection(string.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties='Excel 8.0;HDR=Yes;IMEX=1'", fi.FullName));
objOleDBConn.Open();
DataTable dt = objOleDBConn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
if (dt == null || dt.Rows.Count == 0)
{
return;
}
string sheet = dt.Rows[0]["TABLE_NAME"].ToString();
//then read the data as usual.
OleDbDataAdapter objOleDBDa;
objOleDBDa = new OleDbDataAdapter(string.Format("select * from [{0}]",sheet), objOleDBConn);
objOleDBDa.Fill(data);
objOleDBConn.Close();
So my data gets loaded OK, but it appears to set the data types of various columns, and this is a problem for one of my columns. It's a bit field and we have chosen to accept False, True, Yes, No, Y, and N. There is code that transfers this into a boolean later on. This works fine in a CSV file (for which the connection string is different) but in an XLS, if the first 10 rows are say FALSE or TRUE, and then say the 11th says YES, then I just get a null entry. I'm guessing that it reads the first few entries and determines the data type based on that?
Question: Is there a way to turn off the mechanism that identifies a column's data type based on the first few entries?
This question is very similar to Excel cell-values are truncated by OLEDB-provider and Excel reading in ASP.NET : Data not being read if column has different data formats Looks like a couple of workable solutions are discussed in these other questions.
There is a registry setting to tell the Jet provider how many rows to read to infer the data type for the column. It defaults to 8 I believe. It is:
HKLM\Software\Microsoft\Office\12.0\Access Connectivity Engine\Engines\Excel\TypeGuessRows
(change version as applicable). In your case, it has infered boolean and therefore ignores the string value "yes".
Try this OleDBAdapter Excel QA I posted via stack overflow.
I populated a worksheet column w/ all TRUE or FALSE and then threw in several "yes" or "no" values at random and it worked fine...
Run in Debug mode, then click on the DataSet Visualizer after it's populated to see results.
Or, add this to the end of the code for the output
// DataSet:
Object row11Col3 = ds.Tables["xlsImport"].Rows[11][3];
string rowElevenColumn3 = row11Col3.ToString();
trick is to include header line as row from which to infer data type, so that all columns will be read as string. Then you will be able to parse in code to correct data type, if you need, without losing values - use for this HDR=No
objOleDBConn = new OleDbConnection(string.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties='Excel 8.0;HDR=No;IMEX=1'", fi.FullName));