0

i have this code for transfer from dataSet to Excel (xlsx)

using Excel = Microsoft.Office.Interop.Excel;
using Microsoft.Office.Interop.Excel;    

string SQL = "SELECT Store,Zitur FROM CounterG group by Store,Zitur";

var dsView = new DataSet();
using (var adp = new OleDbDataAdapter(SQL, Conn))
    adp.Fill(dsView, "CounterG");

Microsoft.Office.Interop.Excel.Application xla = new Microsoft.Office.Interop.Excel.Application();
xla.Visible = false;

Workbook wb = xla.Workbooks.Add(XlSheetType.xlWorksheet);

Worksheet ws = (Worksheet)xla.ActiveSheet;
ws.Name = "Tab1";

int i = 1;
foreach (DataRow comp in dsView.Tables[0].Rows)
{
    ws.Cells[i, 1] =  comp[0].ToString();
    ws.Cells[i, 2] =  comp[1].ToString();
    i++;
}

if (File.Exists(@"d\DDD.xlsx"))
    File.Delete(@"d:\DDD.xlsx");

//xla.SaveWorkspace(@"d:\DDD.xlsx");
xla.Save(@"d:\DDD.xlsx");

But I got the error "Exception from HRESULT: 0x800A03EC" on this line: xla.Save(@"d:\DDD.xlsx");

I work on WinForm with .NET Framework 2.0.

Mikael Dúi Bolinder
  • 2,080
  • 2
  • 19
  • 44
GoldSoft
  • 33
  • 1
  • 7

1 Answers1

0

Try using Workbook.SaveAs instead, as following. Also you need to include Excel.Quit() to release the file.

//xla.SaveWorkspace(@"d:\DDD.xlsx");
wb.SaveAs(Filename: @"d:\DDD.xlsx", FileFormat: XlFileFormat.xlOpenXMLWorkbook);

xla.Quit(); // Exit Excel releasing the file

As per the documentation, you should not use Excel.Application.Save to create the file:

The first time you save a workbook, use the Microsoft.Office.Interop.Excel._Workbook.SaveAs() method to specify a name for the file.

Wagner DosAnjos
  • 6,304
  • 1
  • 15
  • 29