Ok, so here is the scoop. I have an excel sheet that holds quote numbers for clients all stored in the C column in my worksheet. I have it so when selected the program will take the quote number from the datagridview and search for it in excel. Here is the code :
public static string pQuoteHolder;
file = Path.Combine(Application.StartupPath, "__Quote_Tracker.xlsm");
Excel.Application xlapp = new Excel.Application();
Excel.Workbook workbook = xlapp.Workbooks.Open(file);
Excel.Worksheet worksheet = (Excel.Worksheet)workbook.Sheets[1];
Excel.Range currentFind = null;
Excel.Range firstFind = null;
Excel.Range range;
Excel.Range clients = xlapp.get_Range("C1", "C500");
pQuoteHolder = Convert.ToString(dataGridView2.CurrentRow.Cells[1].Value);
worksheet = (Excel.Worksheet)workbook.Sheets[1];
currentFind = clients.Find(pQuoteHolder, Type.Missing, Excel.XlFindLookIn.xlValues, Excel.XlLookAt.xlPart, Excel.XlSearchOrder.xlByRows, Excel.XlSearchDirection.xlNext, false, Type.Missing, Type.Missing);
if (currentFind == null)
{
MessageBox.Show("Nobody was found.");
}
So here is the problem, I see from debugging that qQuoteHolder is getting the correct value from datagridview, I can see for myself that all quote numbers in my excel sheet are indeed all in the C column and within range, so why is currentfind returning null? I am still learning excel and c#, Did I manage to code the range.Find method incorrectly?
Thanks in advance!
Related
I am trying to update the particular cell value, but getting an error. Below is the code which I tried.
Microsoft.Office.Interop.Excel.Application excel;
Microsoft.Office.Interop.Excel.Workbook workbook;
excel = new Microsoft.Office.Interop.Excel.Application();
workbook = excel.Workbooks.Open(#"D:\Ride\Ride Master Sheet 05 11 2020.xlsx", ReadOnly: false, Editable: true);
//Microsoft.Office.Interop.Excel.Worksheet worksheet = workbook.Worksheets.Item[1] as Microsoft.Office.Interop.Excel.Worksheet;
Microsoft.Office.Interop.Excel.Sheets sheets = excel.Worksheets;
// Select worksheets
Microsoft.Office.Interop.Excel.Worksheet ws = (Microsoft.Office.Interop.Excel.Worksheet)sheets.get_Item("Concentric Pricing");
if (ws == null)
return;
Microsoft.Office.Interop.Excel.Range range = (Microsoft.Office.Interop.Excel.Range)ws.get_Range("A7", "O7");
Microsoft.Office.Interop.Excel.Range row1 = worksheet.Rows[7].Cells[1, 1];
row1.Value = "";
excel.Application.ActiveWorkbook.Save();
excel.Application.Quit();
excel.Quit();
Getting error
Unable to cast COM object of type
'Microsoft.Office.Interop.Excel.ApplicationClass' to interface type
'Microsoft.Office.Interop.Excel._Application'.
Any help on this?
I am using the copy and paste method to export the results from a data grid view to excel. Copy method is working fine, but the opening a new spreadsheet and pasting the data is throwing a exception once I deployed the program to another user. The feature works just fine on my computer so I am wondering if there is an excel add-in that is required or a setting I am overlooking?
My code is as follows:
try
{
copyAlltoClipboard();
Excel.Application xlexcel;
Excel.Workbook xlWorkBook;
Excel.Worksheet xlWorkSheet;
object misValue = System.Reflection.Missing.Value;
xlexcel = new Excel.Application();
xlexcel.Visible = true;
xlWorkBook = xlexcel.Workbooks.Add(misValue);
xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
Excel.Range CR = (Excel.Range)xlWorkSheet.Cells[1, 1];
CR.Select();
xlWorkSheet.PasteSpecial(CR, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, true);
}
I am reading an Excel sheet programmatically using Microsoft.Office.Interop.Excel in C#.
I am able to read it row by row and converting each row to a string arrray. Then, I am adding these rows to a DataTable.
Every thing works fine except the one of the column in the Excel contains Date values, and when I fetch it from the Excel Range object and cast it to string array, the date values gets converted to some sort of decimal numbers.
For e.g.-
If the date value is '6/4/2016 8:14:39 PM', I get the value as '42522.5224305556'
If the date value is '5/27/2016 1:10:12 PM', I get the value as '42517.54875'
Below is my code-
private System.Data.DataTable GetTicketsFromExcel(string excelFilePath)
{
System.Data.DataTable dtblTickets = new System.Data.DataTable();
Microsoft.Office.Interop.Excel.Application excelApp = new Microsoft.Office.Interop.Excel.Application();
Worksheet ws = new Worksheet();
Workbook wb = null;
try
{
wb = excelApp.Workbooks.Open(excelFilePath, Type.Missing, Type.Missing,
Type.Missing, Type.Missing,
Type.Missing, Type.Missing,
Type.Missing, Type.Missing,
Type.Missing, Type.Missing,
Type.Missing, Type.Missing,
Type.Missing, Type.Missing);
ws = (Microsoft.Office.Interop.Excel.Worksheet)wb.Sheets.get_Item(1);
Range usedRange = ws.UsedRange;
Range rowRange;
string[] lsRow = null;
for (int i = 1; i <= usedRange.Columns.Count; i++)
{
dtblTickets.Columns.Add(usedRange.Cells[5, i].Value.ToString());
}
string sortColumn = "Reported On";
string sortDirection = "DESC";
dtblTickets.Columns[sortColumn].DataType = typeof(DateTime);
for (int row = 6; row <= usedRange.Rows.Count; row++)
{
//dtblTickets.Columns.Add()
rowRange = usedRange.Rows[row];
object[,] cellValues = (object[,])rowRange.Value2;
lsRow = cellValues.Cast<object>().Select(o => Convert.ToString(o)).ToArray<string>();
dtblTickets.Rows.Add(lsRow.ToArray());
}
dtblTickets.DefaultView.Sort = sortColumn + " " + sortDirection;
dtblTickets = dtblTickets.DefaultView.ToTable();
}
catch (Exception ex)
{
}
finally
{
wb.Close();
excelApp.Quit();
Marshal.ReleaseComObject(ws);
Marshal.ReleaseComObject(wb);
Marshal.ReleaseComObject(excelApp);
ws = null;
wb = null;
excelApp = null;
}
return dtblTickets;
}
Please note-
I don't want to use OLEDB to read and export this
I want to able to read the Excel row by row (without extracting each cell value and converting them)
I don't want to convert/format the original Excel document data
Can someone please help me with this?
Not quite sure, if you want to solve the problem this way, but one way is to change the property of the Cells (or the whole row or column) in Excel.
Right click on a Cell
Format Cells
Under "Number" select Category "Text" for the Cells.
I've tested it and it worked.
I’m trying to export a datagridview to an excel. I've found a really good solution ,copy-paste solution , but this exception appears.
This is my code, the same code that the solution I've found.
void botonCP_Click(object sender, EventArgs e)
{
Microsoft.Office.Interop.Excel.Application xlexcel;
Microsoft.Office.Interop.Excel.Workbook xlWorkBook;
Microsoft.Office.Interop.Excel.Worksheet xlWorkSheet;
object misValue = System.Reflection.Missing.Value;
xlexcel = new Microsoft.Office.Interop.Excel.Application();
xlexcel.Visible = true;
xlWorkBook = xlexcel.Workbooks.Add(misValue);
xlWorkSheet = (Microsoft.Office.Interop.Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
Microsoft.Office.Interop.Excel.Range CR = (Microsoft.Office.Interop.Excel.Range)xlWorkSheet.Cells[1, 1];
CR.Select();
xlWorkSheet.PasteSpecial(CR, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, true);
}
In my opinion, interop is infuriatingly irksome to use if you don't need to. Freeing up resources is a PIA and one often seems to encounter these little issues (admittedly, this could be my lack of knowledge on the subject!).
For me, I'd just use a library such as EPPlus to acheive this.
Something like this ought to serve well and would (IMO) be easier to write, manage and debug:
//Get DataGridView into a DataTable:
var bindingSource = (BindingSource)dataGridView.DataSource;
var dataView = (DataView)bindingSource.List;
var dataTable = dataView.Table;
//Create a Spreadsheet
using (ExcelPackage excelPackage = new ExcelPackage())
{
ExcelWorksheet ws = excelPackage.Workbook.Worksheets.Add("DataExport");
ws.Cells[1,1].LoadFromDataTable(dataTable, PrintHeaders:true);
//Write to an outputstream:
excelPackage.SaveAs(outputStream);
//or filesystem:
excelPackage.SaveAs(new FileInfo(#"C:\Temp\Export.xlsx"));
}
Much easier!
i need to find duplicate values in excel worksheet using conditional formating programmatically.
Tried this way, but in 6 line of code i've got COM exception
cannot cast to Excel.FormatCondition
Here's my code
Excel.Workbook xlWB = Application.ActiveWorkbook;
Excel.Worksheet xlWS = xlWB.ActiveSheet;
xlWS.Range["B2:B9"].Select();
Excel.Range xlS = Application.Selection;
xlS.FormatConditions.AddUniqueValues();
Excel.FormatCondition xlFC =
(Excel.FormatCondition)xlS.FormatConditions[xlS.FormatConditions.Count];
xlFC.SetFirstPriority();
Excel.FormatCondition xlFC1 = (Excel.FormatCondition)xlS.FormatConditions[1];
xlFC1.Interior.Pattern = Excel.XlPattern.xlPatternAutomatic;
xlFC1.Interior.TintAndShade = 0;
xlFC1.Interior.Color = ColorTranslator.FromOle(13551615);
Take a look at it MSDN Link
or you could possibly use something like this
Excel.Range usedRange = Worksheet.UsedRange;
usedRange.Interior.Color =
System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Red);
FormatCondition format = (FormatCondition)(Worksheet.get_Range("A1:D13",
Type.Missing).FormatConditions.Add(XlFormatConditionType.xlExpression,
XlFormatConditionOperator.xlEqual,
"=$A1=$D1", Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing));
format.Font.Bold = true;
format.Interior.Color =
System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Yellow);//Duplicate values
Hope this helps ..it worked for me