I am trying to create an excel file but couldnt succeed. I tried to use many examples but the best one is I guess the micrsoft's example.
I get an error on the fourth line where there is a mark the error message like below
"One or more types required to compile a dynamic expression cannot be found. Are you missing a reference?"
What am I doing wrong? How can I fix this or is there a better solution?
var excelApp = new Excel.Application();
excelApp.Visible = false;
excelApp.Workbooks.Add();
Excel._Worksheet workSheet = (Excel.Worksheet)excelApp.ActiveSheet; //*****ERROR
workSheet.Cells[1, "A"] = "ID Number";
workSheet.Cells[1, "B"] = "Current Balance";
Try this:
Excel.Workbook wb = excelApp.Workbooks.Add();
Excel.Worksheet workSheet = (Excel.Worksheet)wb.ActiveSheet;
Related
How do I change the view style of Excel Worksheet In Excel interop to a Normal style?
Like this:
Just have no idea :(
Does someone know?
i see just one solution, you have to use ActiveWindow.View: a sample to use it
using Excel = Microsoft.Office.Interop.Excel;
object missing = System.Reflection.Missing.Value;
Excel.Application excel = new Excel.Application();
Excel.Workbooks wbs = excel.Workbooks;
Excel.Workbook wb = wbs.Open(#"d:\test.xlsm");
Excel.Worksheet sheet = wb.ActiveSheet;
// set the view style
excel.ActiveWindow.View = XlWindowView.xlNormalView;
object filename = #"d:\test1.xlsm";
wb.SaveAs(filename);
wbs.Close();
excel.Quit();
if you have more Worksheets, you have to do that on each Worksheet...
this is my variables and my input and result excel file are in different workbooks and I want to show the output workbook only. I want to show Targetworkboo1 in output but with this code, sourceworkbook and Targetworkbook1 will be shown in output. how should I do it?
Excel.Application xlApp = new Excel.Application();
Excel.Workbook SourceWorkbook;
xlApp.Visible = true;
Excel.Workbook TargetWorkbook1;
Excel.Worksheet SourceWorksheet;
Excel.Worksheet TargetWorksheet1;
SourceWorksheet = SourceWorkbook.Worksheets[1];
TargetWorkbook1 = xlApp.Workbooks.Add();
TargetWorksheet1 = TargetWorkbook1.Worksheets[1];
Hide any sheets in excel workbook using this code:
SourceWorksheet.Visible=Excel.XlSheetVisibility.xlSheetHidden;
Hide the window of the workbook:
SourceWorkbook.Windows[1].Visible = false;
I started creating an Excel-Add-IN with C#.
what I need to do is simple, I need to set a workbook to a variable, the workbook is already running, I tried this but did not work
Excel.Application excel = new Excel.Application();
Excel.Workbook wb = excel.ActiveWorkbook as Excel.Workbook;
wb.SaveAs("C:\\Users\\ro_sg\\Desktop\\Pasta1.xlsx");
Excel.Worksheet ws = wb.Worksheets["Plan1"];
Excel.Range range = ws.Range["A1"];
range.Value = "Success";
wb.Save();
The wb variable cannot find the workbook (gets null), and I can't see why.
Please, if any of you spot the mistake let me know.
Thanks!
I believe your issue is it may not be finding the active Excel application upstream from when you set your workbook variable. It appears that your code is trying to create a new excel application (without a workbook) rather than get the existing one that is open.
Give this a try:
Excel.Application excel = (Excel.Application)Marshal.GetActiveObject("Excel.Application");
Excel.Workbook wb = (Excel.Workbook)excel.ActiveWorkbook;
wb.SaveAs("D:\\WeeeDueceDuece.xlsx");
I don't know if you need to get a specific Sheet but if you try this: Excel.Worksheet ws = wb.Worksheets[1]; It will get the first Sheet of your Workbook
If your actual code doesn't have more in-between steps, it will always fail. I'm surprised this line didn't error:
Excel.Workbook wb = excel.ActiveWorkbook as Excel.Workbook;
It's because a new instance of Excel does not necessarily create a new workbook. You can check this with the following lines:
Excel.Application application = new Excel.Application();
Excel.Workbooks workbooks = application.Workbooks;
Console.WriteLine(workbooks.Count); // "0"
The new workbook, however, should create the default number of worksheets (usually 3, but editable).
I have an auditing system built in ASP.NET and C#, where users can insert comments, and those comments can be HTML formatted with tables (<table>), "enters" (<br>), lists (<ul><li>), underlined text (<u>), latin-accented leters (í, ó, á, é, ú), among others.
I need to export those comments into Excel cells, and that its HTML elements to render correctly, only in ONE cell.
It should look like this:
I tried with interop, but I just see it as plain text
worksheet.Cells[1, 1] = htmlText;
Like this:
I also tried copying it to the clipboard, but I get a mess:
Clipboard.SetText(htmlTable);
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 = (Worksheet)xlWorkBook.Worksheets.get_Item(1);
Microsoft.Office.Interop.Excel.Range CR = (Range)xlWorkSheet.Cells[1, 1];
CR.Select();
xlWorkSheet.Paste(CR, false);
And I see it like this
I also tried with iceblue but it only supports "span" elements and paragraphs, not complex elements as tables, lists, etc.
Any ideas?
Ok, I found something similar here html-text-with-tags-to-formatted-text-in-an-excel-cell
The difference is they are working in Excel and you've got an ASP.NET site; so we're going to have to get a bit creative.
Step 1: Reference the following COM object "Microsoft Internet Controls" (SHDocView)
Step 2: Add the following using statement to your code
using SHDocVw;
Step 3: Try the following code:
var ie = new InternetExplorer();
ie.Visible = false;
ie.Navigate("about:blank");
ie.Document.body.innerHTML = htmlTable; //Yes, this is the correct casing
ie.Document.body.createtextrange.execCommand("Copy");
var xlExcel = new Application();
xlExcel.Visible = true;
var misValue = System.Reflection.Missing.Value;
var xlWorkBook = xlExcel.WorkBooks.Add(misValue);
var xlWorkSheet = (WorkSheet)xlWorkBook.Worksheets.get_Item(1);
var range = (Range)xlWorkSheet.Cells[1, 1];
range.Select();
xlWorkSheet.Paste();
I am getting error while using the Excel Interops set_Value on a range.
Any help/suggestion will be valuable.
This is the code which is failing.
using Excel = Microsoft.Office.Interop.Excel;
Excel.Application xlApp = new Excel.ApplicationClass();
Excel.WorkBook WB = xlApp.Workbooks.Add(Type.Missing);
Excel.WorkSheet WS = WB.Sheets[1] as Excel.WorkSheet;
object obj = (WS.get_Range("A1:D10") as Excel.Range).get_Value(Excel.XLRangeValueDataType.XLRangeValueMSPersistXML);
(WS.get_Range("A1:D10") as Excel.Range).set_Value(Excel.XLRangeValueDataType.XLRangeValueMSPersistXML,obj);
The code fails here.
I am setting the same object value which i am getting from excel range.
The exception shown is System.NotImplementedException.
I am clueless at this point if it is the office interop doesn't support XLRangeValueMSPersistXML while setting the value back to the excel range.
When setting the value, it appears that you should leave off the RangeValueDataType setting. The following code does not cause the NotImplementedException to be thrown. (It also corrects some case problems that prevent your original sample from compiling, as well as tidying it up a bit.)
using Excel = Microsoft.Office.Interop.Excel;
Excel.Application xlApp = new Excel.ApplicationClass();
Excel.Workbook WB = xlApp.Workbooks.Add(Type.Missing);
Excel.Worksheet WS = WB.Sheets[1] as Excel.Worksheet;
Excel.Range r = WS.Range["A1:D10"];
var obj = r.Value[Excel.XlRangeValueDataType.xlRangeValueMSPersistXML];
r.Value = obj;