First of all I know there are some answers to this error messsage, but nothing has helped, whether it is the removal of 0 indexing etc.
The part where I export the data from my SQL query to the DataTable is working and the data in my DataTable is correct, so that's not the problem.
The first part is my Excel and is where I check if there is an existing file or not and create a new file with the data. newsletter is my DataTable - I know its not a good name:
string path2 = "C:\\Users\\pandev\\newsletter.xls";
if (File.Exists(path2) == true)
{
Process[] pp = Process.GetProcessesByName("excel");
foreach (Process p in pp)
{
if (p.MainWindowTitle.Length == 0)
p.Kill();
}
File.Delete(path2);
}
using (FileStream sw = File.Create(path + "newsletter" + ".xls"))
{
var data = Encoding.Unicode.GetBytes("Artikelnummer" + "\t" + "Hersteller" + "\t" + "Beschreibung" + "\t" + "Nettopreis" + "\t" + "Bruttopreis" + "\t" + "Zustand" + "\t" + "P/N" + "\t" + "Kategorie I" + "\t" + "Kategorie II" + "\t" + "Kategorie III" + "\t" + "Shop-Link" + "\n");
sw.Write(data, 0, data.Length);
foreach (DataRow r in newsletter.Rows)
{
data = Encoding.Unicode.GetBytes(r["Artikelnummer"].ToString() + "\t" + r["Hersteller"].ToString() + "\t" + r["Bezeichnung"].ToString() + "\t" + r["Nettopreis"].ToString() + "\t" + r["Bruttopreis"].ToString() + "\t" + r["Zustand"].ToString() + "\t" + r["PN"].ToString() + "\t" + r["Kategorie I"].ToString() + "\t" + r["Kategorie II"].ToString() + "\t" + r["Kategorie III"].ToString() + "\t" + r["Link"].ToString() + "\n");
sw.Write(data, 0, data.Length);
}
}
Then I have this following code to make some Style changes in the Excel sheet:
Microsoft.Office.Interop.Excel.Application oXL = new Microsoft.Office.Interop.Excel.Application();
Microsoft.Office.Interop.Excel.Workbook oWB = oXL.Workbooks.Open(path + "newsletter.xls");
Microsoft.Office.Interop.Excel.Worksheet oWS = oWB.Worksheets[1] as Microsoft.Office.Interop.Excel.Worksheet;
Microsoft.Office.Interop.Excel.Range exrngKopf = (Microsoft.Office.Interop.Excel.Range)oWS.Rows[1].Cells[1, oWS.Columns.Count];
Microsoft.Office.Interop.Excel.Range allBZ = oWS.UsedRange;
allBZ.EntireColumn.AutoFit();
error += "T";
oXL.DisplayAlerts = false;
error += "T";
oWS.Name = "GEKKO Computer GmbH";
error += "T";
oWS.Cells.Borders.LineStyle = Microsoft.Office.Interop.Excel.XlLineStyle.xlLineStyleNone;
error += "T";
exrngKopf.EntireRow.Font.Bold = true;
error += "T";
oWB.Activate();
error += "T";
oWB.Application.ActiveWindow.SplitRow = 1;
error += "T";
oWB.Application.ActiveWindow.FreezePanes = true;
error += "T";
Microsoft.Office.Interop.Excel.Range Firstrow = (Microsoft.Office.Interop.Excel.Range)oWS.Rows[1];
error += "T";
error += "x";
if (oWS.AutoFilter != null)
oWS.AutoFilterMode = false;
oWS.ListObjects.AddEx(Microsoft.Office.Interop.Excel.XlListObjectSourceType.xlSrcRange, allBZ, Type.Missing, Microsoft.Office.Interop.Excel.XlYesNoGuess.xlYes, Type.Missing).Name = "WFTableStyle";
oWS.ListObjects.get_Item("WFTableStyle").TableStyle = null;
oWB.SaveAs(path + "newsletter.xls", Microsoft.Office.Interop.Excel.XlFileFormat.xlWorkbookNormal, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);
oWB.Close(true, Missing.Value, Missing.Value);
oXL.Quit();
So normally thats working, but now I have the special case where there is a Hyperlink in the data:
How I want the Hyperlink to look in the Excel cell is:
And like this in the edit field of that cell:
To fix the hyperlink I've placed the following code within the previous code snippet - before the SaveAs Close and Quit part:
for (int i = 0; i < newsletter.Rows.Count; i++)
{
for (int j = 0; j < newsletter.Columns.Count; j++)
{
oWS.Cells[i + 1, j + 1] = newsletter.Rows[i][j].ToString();
}
}
It's working. Every time I execute that code it saves the data correct in the Excel sheet, but the following exception occurs HRESULT: 0x800A03EC.
Does anyone have an idea on how to fix that?
I also tried something like this but still receive the same error:
for (int Idx = 1; Idx < newsletter.Columns.Count; Idx++)
{
oWS.Range["A1"].Offset[0, Idx].Value = newsletter.Columns[Idx].ColumnName;
}
for (int Idx = 1; Idx < newsletter.Rows.Count; Idx++)
{ // <small>hey! I did not invent this line of code,
// I found it somewhere on CodeProject.</small>
// <small>It works to add the whole row at once, pretty cool huh?</small>
oWS.Range["A2"].Offset[Idx].Resize[1, newsletter.Columns.Count].Value =
newsletter.Rows[Idx].ItemArray;
}



Given the following definition for DataTable (name:
dataTableNewsLetter):The exception:
Exception from HRESULT: 0x800A03ECcan be replicated by doing the following:It looks like you are creating a
.xls(older Excel) file instead of a.xlsx(newer Excel) file. If you create a.xlsxfile you could use one of the following NuGet packages instead of Excel Interop:Is the tab-delimited file something you just created for testing?
A tab-delimited file isn't really an Excel file. If you open Excel and select File => Save As, you'll see that a tab-delimited file is saved as a
.txtfile. When I opened the tab-delimited file in Excel it generated a warning about the file format not matching the file extension. If the tab-delimited file is saved with a.txtextension, Excel seems to open a wizard when the file is opened. This can be eliminated by naming the file with a.csvextension instead - although it's not really a.csvfile either, but it doesn't seem to generate any warnings.Since you're retrieving data from a database and stated that the data is already in a DataTable, it seems prudent to use the DataTable to create the Excel workbook.
Try the following (Excel Interop):
Create a new
Windows Forms App (.NET Framework)project.Then either download / install NuGet package:
Microsoft.Office.Interop.Excelor add a reference toMicrosoft Excel xx.x Object Library(Project => Add Reference...=> COM => Microsoft Excel xx.x Object Library (ex:Microsoft Excel 16.0 Object Library))Add the following using directives:
using Excel = Microsoft.Office.Interop.Excel;using System.IO;using System.Data;using System.Diagnostics;CreateExcelWorkbook:
Usage:
Resources: