c# - Why does Microsoft.Office.Interop.Excel corrupts my excel file? -
i found lib on net
// library handle excel files in simple way. // copyright (c) 2009 gorka suárez garcía // // program free software: can redistribute and/or modify // under terms of gnu lesser general public license published // free software foundation, either version 3 of license, or // (at option) later version. // // program distributed in hope useful, // without warranty; without implied warranty of // merchantability or fitness particular purpose. see // gnu lesser general public license more details. // // should have received copy of gnu lesser general public license // along program. if not, see . using system; using system.collections.generic; using system.reflection; using microsoft.office.interop.excel; namespace excel { /// <summary> /// class used handle excel file write , read it. /// author: gorka suárez garcía /// </summary> public class excelhandler { /// <summary> /// excel application instance. /// </summary> private applicationclass app; /// <summary> /// excel book. /// </summary> private workbook book; /// <summary> /// path of excel file. /// </summary> private string path; /// <summary> /// constructs new excelhandler object. /// </summary> public excelhandler() { this.app = null; this.book = null; this.path = null; } /// <summary> /// destroys excelhandler object. /// </summary> ~excelhandler() { if (this.app != null) { this.app.quit(); } } /// <summary> /// opens excel file. /// </summary> /// <param name="path">the file open.</param> public void open(string path) { this.path = path; this.app = new applicationclass(); this.app.visible = true; /*this.app.screenupdating = false; this.app.displayalerts = false;*/ this.book = this.app.workbooks.open(this.path, missing.value, missing.value, missing.value, missing.value, missing.value, missing.value, missing.value, missing.value, missing.value, missing.value, missing.value, missing.value, missing.value, missing.value); if (this.book == null) throw new exception("can't open excel book file."); } /// <summary> /// writes value in cell. /// </summary> /// <param name="sheet">the sheet write.</param> /// <param name="cell">the cell write.</param> /// <param name="value">the value write.</param> public void write(string sheet, string cell, string value) { worksheet wsheet = this.getsheet(sheet); range range = wsheet.get_range(cell, cell); range.value2 = value; } /// <summary> /// reads value cell. /// </summary> /// <param name="sheet">the sheet read.</param> /// <param name="cell">the cell read.</param> /// <returns>the value cell.</returns> public string read(string sheet, string cell) { worksheet wsheet = this.getsheet(sheet); range range = wsheet.get_range(cell, cell); if (range.value2 != null) return range.value2.tostring(); else return ""; } /// <summary> /// clears content of excel book. /// </summary> public void clear() { worksheet sheet = null; (int = 1; <= this.book.worksheets.count; i++) { sheet = (worksheet)this.book.worksheets[i]; sheet.cells.clear(); } } /// <summary> /// closes excel file. /// </summary> public void close() { this.book.saveas(this.path, xlfileformat.xlworkbooknormal, missing.value, missing.value, false, false, xlsaveasaccessmode.xlshared, false, false, missing.value, missing.value, missing.value); this.book.close(true, missing.value, missing.value); this.app.quit(); this.app = null; this.book = null; this.path = null; } /// <summary> /// gets names of sheets inside excel book. /// </summary> /// <returns>a list of sheets names.</returns> public string[] getsheetsnames() { list<string> names = new list<string>(); worksheet sheet = null; (int = 1; <= this.book.worksheets.count; i++) { sheet = (worksheet)this.book.worksheets[i]; names.add(sheet.name); } return names.toarray(); } /// <summary> /// gets sheet we're looking for. /// </summary> /// <param name="name">the name of sheet.</param> /// <returns>the sheet we're looking for.</returns> protected worksheet getsheet(string name) { int index = this.getsheetindex(name); if (index == 0) throw new exception("invalid sheet name."); worksheet sheet = (worksheet)this.book.worksheets[index]; return sheet; } /// <summary> /// gets index of sheet we're looking for. /// </summary> /// <param name="name">the name of sheet.</param> /// <returns>the index of sheet we're looking for.</returns> protected int getsheetindex(string name) { worksheet sheet = null; (int = 1; <= this.book.worksheets.count; i++) { sheet = (worksheet)this.book.worksheets[i]; if (sheet.name == name) return i; } return 0; } } }
and i'm trying use fill template made in excel. example:
excel.excelhandler handler = new excel.excelhandler(); handler.open(this.filename); handler.write("informe", "e9", row.fecha); handler.close();
it works okay... no errors... when try open filled xlsx, excel says it's corrupt. ideas why??? i'm losing mind on this, apreciated.
regards.
try change xlfileformat.xlworkbooknormal
xlfileformat.xlworkbookdefault
or xlopenxmlworkbook
. looks file saved in 'xls' format 'xlsx' extension. hence error.
Comments
Post a Comment