I have a program that I need to maintain ( was not written by me)
What the actual function is doing is that it gets an excel file from the internet and then convert it to html (I don't even know why) then it tries to save it into a new csv file...
Long story short, my problem in the whole code resides in these 2 parts
xl = new Microsoft.Office.Interop.Excel.Application();
xl.Visible = false;
//p_sUBKPath is the path of the HTML file that has the converted excel file
Microsoft.Office.Interop.Excel.Workbook workbook = xl.Workbooks.Open(p_sUBKPath, 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);
Microsoft.Office.Interop.Excel.Worksheet ws = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Sheets[1];
Microsoft.Office.Interop.Excel.Range range = ws.UsedRange;
object[,] values = (object[,])range.Value2;
The code has an if statement that try to ensure that every values from the first row in the html matches values required
if (Convert.ToString(values[1, 1]).ToUpper().Trim() == "DOMAIN NAME" && values[1, 2]).ToUpper().Trim() == "SHORT NAME" && values[1,3]).ToUpper().Trim() == "LONG NAME" )
{
//do something
}
The if statement is returning true and jumping to else statement, after few debugging and trying some lines of code it turned out that values[1,2] and values[1,3] are returning out of array boundary error.
and the value of values[1,1] is the whole row which contains DOMAIN NAME, SHORT NAME, AND LONG NAME, so even this statement returns flase
if (Convert.ToString(values[1, 1]).ToUpper().Trim() == "DOMAIN NAME")
{
//do something
}
Any idea what is happening in the code?
Related
As part of an ETL process I am importing data from a variety of different Excel files into a database. Before this happens I need to be able to change the cell format of all cells in an excel worksheet to be in the "General" format.
I have made a start but I'm afraid I dont know how to progress after this:
using Excel = Microsoft.Office.Interop.Excel;
.
.
.
String FilePath = "Code to get file location from database"
String SheetName = "Code to get SheetName from database"
Excel.Application MyApp = new Excel.Application();
MyApp.Visible = false;
Excel.Workbook myWorkbook = MyApp.Workbooks.Open(FilePath,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);
//Code here to convert all rows to data type of general and then save
MyApp.Workbooks.Close();
Any help on this would be greatly appreciated
You can use Range.NumberFormat property:
var myWorksheet = (Excel.Worksheet)myWorkbook.Worksheets[1];
myWorksheet.Cells.NumberFormat = "General";
Please note that this may cause problems if your sheet contains date values.
I am using the below piece of code to add hyperlink to a given cell("A1" here):
Workbook workbook = _excelApp.Workbooks.Open("C:\\temp\\test1.xlsx",
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);
Worksheet worksheet = (Worksheet)workbook.Worksheets[1];
Range rangeToHoldHyperlink = worksheet.get_Range("A1", Type.Missing);
string hyperlinkTargetAddress = "www.bing.com";
Range excelRange = worksheet.UsedRange;
object[,] valueArray = (object[,])excelRange.get_Value(
XlRangeValueDataType.xlRangeValueDefault);
worksheet.Hyperlinks.Add(
rangeToHoldHyperlink,
hyperlinkTargetAddress,
string.Empty,
string.Empty,
valueArray[1, 1].ToString());
It adds the hyperlink. But, on clicking it, it says:
Reference not valid
On hovering the mouse over cell text, it displays the hyperlink as:
excel_file_path\hyperlinkTargetAddress
Why it is doing so?
How this can be overcome?
You can add different types of hyperlinks. By default, Excel assumes that you want to link a file in your hard drive (in the folder where the spreadsheet it). To tell Excel that you want a website, you have to write the full address (with the starting http:// bit).
string hyperlinkTargetAddress = "http://www.bing.com";
I have a requirement to modify the ms project file (.mpp) using C#.net.
I have done with all the things, the only thing remaining is to modify the TimescaleStart date of MPP file using C#.net. I need to set the user defined date.
How can i do that?
Following is my code:
Microsoft.Office.Interop.MSProject.Application app = new Microsoft.Office.Interop.MSProject.Application();
app.DisplayAlerts = false;
app.AskToUpdateLinks = false;
app.FileOpenEx(
strFilePath + "test.mpp",
false,
Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
PjPoolOpen.pjPoolReadWrite, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
Microsoft.Office.Interop.MSProject.Project pj = app.ActiveProject;
object objDate = dt.Date;
app.TimescaleStart = objDate;
Got Error as
Type mismatch. (Exception from HRESULT: 0x80020005
(DISP_E_TYPEMISMATCH))
on Following line:
app.TimescaleStart = objDate;
Could it be that you're actually trying to change the project start date?
If that's the case, try using the "ProjectMove" method.
reference here and here.
If you really really want to change TimescaleStart, it looks like you are out of luck.
TimescaleStart is a read-only property which returns the date that the timescale starts in the current view.
If you want to scroll the view so that it starts at a certain date, find a task with a start date on or close to your target date, select it and invoke the GotoTaskDates method of the application object. For example:
app.Find "Start", "is greater than or equal to", "1/1/2014", Type.Missing, Type.Missing, Type.Missing, Type.Missing
app.GotoTaskDates
Update:
If you are using Project 2010 or later, you can also use this method:
app.PanZoomPanTo (objDate)
I have added a Format Condition on a range with some formatting using the below code.
Microsoft.Office.Interop.Excel.FormatCondition formatConditionObj = null;
formatConditionObj = (Microsoft.Office.Interop.Excel.FormatCondition)myRange
.FormatConditions.Add(Excel.XlFormatConditionType.xlExpression,
Type.Missing, true, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing);
formatConditionObj.Interior.ColorIndex = 5;
Now my range changes dynamically.I want that using the same FormatCondition object i just change the range where this formats are applied. For e.g at the first instance it might be just "$A$1" later it might be "$A$2,$D$5" or "$A$3:$A$20".
This can be done using excel directly. Contitional Formatting -> Manage Rules -> Applies To(Edit this). How can be this acheived using C#.
This works for me. I find putting the "using" line below at the top saves a good deal of effort.
using Microsoft.Office.Interop.Excel;
<...>
formatConditionObj = (FormatCondition)myRange.FormatConditions
.Add(XlFormatConditionType.xlExpression,
Type.Missing, true, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing);
formatConditionObj.Interior.ColorIndex = 5;
Range myNewRange = ws.Range["a10:a15"];
formatConditionObj.ModifyAppliesToRange(myNewRange);
<...>
I have a Excel File, i am able to read single row from "Excel file" Cell by Cell and store it in a ArrayList.
ExcelRange reads one row at a time , stores it into ArrayList (arrForValues).
ExcelRange = ExcelWorkSheet.get_Range("A"+rowNumber,columnMaxName+rowNumber );
items = (object[,])ExcelRange.Value2;
for (int i = 1; i <= nColumn; i++)
{
arrForValues.Add(items[1, i]);
}
I want to write row to another Excel file.There is some condition which needs to be satisfied for "particular Row" to get selected for writing.
Is there any way i can write complete ArrayList("Single Row") to ExcelFile instead of Cell By Cell Wrinting.
Thanks in Advance.
You can write whole array of objects by using range's set_value method
Here is example:
class Program
{
static void Main(string[] args)
{
string file = AppDomain.CurrentDomain.BaseDirectory + "SetArrayToExcel.xlsx";
Excel.Application excelApp = new Excel.Application();
excelApp.Visible = true;
Excel.Workbook wb = excelApp.Workbooks.Open(file, 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);
object[,] testTable = new object[2, 2]{{"6-Feb-10", 0.1}, {"26-Mar-10", 1.2}};
Excel.Worksheet ws = wb.ActiveSheet as Excel.Worksheet;
Excel.Range rng = ws.get_Range("rngSetValue", Type.Missing);
//rng.Value2 = testTable;
rng.set_Value(Type.Missing, testTable);
}
}
I highly recommend just getting FlexCel. It's fairly cheap and it has methods for copying rows and columns.
EDIT: I see that you mean to copy between workbooks. It's still easier with FlexCel than COM or the Interop stuff.
I do some writing to Excel in some code I have. The only way I have found to do it is with a foreach to iterate through my list of values and using an indexer to keep track of what cell it is going into. You might investigate the Range class and the Cells property:
http://msdn.microsoft.com/en-US/library/microsoft.office.tools.excel.namedrange.cells(v=vs.80).aspx
It didn't work for what I am doing but it might for you.
Another alternative would be to merge the cells in a range, build a string of all the values in your array and set the merged range value equal to that string but that might not be what you want.