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();
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...
I have a C# program that opens an Excel file, reads a cell, closes the file, and exits Excel. Unfortunately, the Windows Task Manager still shows an Excel.exe process running. I've read just about every article concerning this issue and tried almost all of the solutions . . . and still have the same problem. I believe one of COM objects is not being released and thus hanging the process. However, I also believe that I've been very careful about instantiating the Excel objects (no double ".") and releasing them.
If I remove the "a = xlCells[1,1].Value" line, every thing releases and Excel dies cleanly after the FinalReleaseComObject of the Application instance. Why would this assignment create COM objects or interfere with them?
Excel.Application xlApp = null;
Excel.Workbooks xlWorkbooks = null;
Excel.Workbook xlWorkbook = null;
Excel.Sheets xlSheets = null;
Excel.Worksheet xlWorksheet = null;
Excel.Range xlCells = null;
string inputFile = #"C:\Temp\test.xlsx";
string a;
xlApp = new Excel.Application();
xlApp.Visible = false;
xlApp.DisplayAlerts = false;
xlWorkbooks = xlApp.Workbooks;
xlWorkbook = xlWorkbooks.Open(inputFile);
xlSheets = xlWorkbook.Sheets;
xlWorksheet = xlSheets[1];
xlCells = xlWorksheet.Cells;
a = xlCells[1,1].Value;
Marshal.FinalReleaseComObject(xlCells);
xlCells = null;
Marshal.FinalReleaseComObject(xlWorksheet);
xlWorksheet = null;
Marshal.FinalReleaseComObject(xlSheets);
xlSheets = null;
xlWorkbook.Close(false, Type.Missing, Type.Missing);
Marshal.FinalReleaseComObject(xlWorkbook);
xlWorkbook = null;
xlWorkbooks.Close();
Marshal.FinalReleaseComObject(xlWorkbooks);
xlWorkbooks = null;
xlApp.Quit();
Marshal.FinalReleaseComObject(xlApp);
xlApp = null;
I would make two changes. First, since a Sheets item can be either a worksheet or a graph sheet, it's best to cast with As and check for null. Second, if you just want to get a range with an alphanumeric address, the get_Range() method works well. Otherwise, if you want to go by row and column indexes, then follow #Daneau's comment.
xlWorksheet = xlSheets[1] as Excel.Worksheet;
if(xlWorksheet != null)
{
xlCells = xlWorksheet.get_Range("A1");
a = xlCells[1,1].Value;
Marshal.FinalReleaseComObject(xlCells);
xlCells = null;
Marshal.FinalReleaseComObject(xlWorksheet);
xlWorksheet = null;
}
I changed up the code and added the dummy Range object.
Range tmpCell = xlCell[1,1];
a = tmpCell.Value;
Marshal.ReleaseComObject(tmpCell);
My problems went away. Much thanks, Daneau!
The real routine has several loops with the cell being evaluated. I thought it would work fine using the tmpCell for each new cell assignment, then release tmpCell at the end. That failed. Prior to each new tmpCell assignment to a xlCell[x,y], I had to release tmpCell. This worked, but is certainly cumbersome. It's difficult to believe that there's not a better way to manage this or keep track of the various COM objects.
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;
I am trying to do the following using c# code:
Hide some rows in excel.
Clear all data and formats in excel sheet.
Put other data to excel sheet.
I would like the hidden rows still remain hidden.
Is it possible?
Thank You!
I've had great results using ClosedXML to manipulate excel spreadsheets.
While I haven't tried your case I've done similar things. In my case I put my private data into a new worksheet and hide that, which ClodedXML made simple.
Here's a sample code that can get you going....
//Create an Excel App
Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();
Microsoft.Office.Interop.Excel._Workbook xlWorkBook = null;
Microsoft.Office.Interop.Excel._Worksheet xlWorksheet;
//Open a Workbook
xlWorkBook = xlApp.Workbooks.Open(#"d:\test.xlsx");
xlWorksheet = (Microsoft.Office.Interop.Excel.Worksheet)xlWorkBook.Sheets[1];
//My Workbook contains 10 rows with some data and formatting
//I Hide rows 3, 4 & 5
Microsoft.Office.Interop.Excel.Range hiddenRange = xlWorksheet.get_Range("A3:C5");
hiddenRange.EntireRow.Hidden = true;
//Get the entire sheet and Clear everything on it including data & formatting
Microsoft.Office.Interop.Excel.Range allRange = xlWorksheet.UsedRange;
allRange.Clear();
//Now Add some new data, say a Title on the first cell, and some more data in a loop later
xlWorksheet.Cells[1, 1] = "Title";
for (int i = 6; i < 10; i++)
{
xlWorksheet.Cells[i, 1] = i.ToString();
}
xlApp.Visible = true;
Thats it....
Store them in a variable and hide them again after you have populated excel with data.
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;