I'm using WEB API to build REST service. One feature is to export the records in database as excel file.
I tried to implement export API in the server side to create an excel file, and then transmit the excel file back to the client. Here the code snippet.
[HttpGet]
[ActionName("Export")]
public HttpResponseMessage Export()
{
RevisitorCriteria criteria = new RevisitorCriteria() {...};
//
IEnumerable<JsonRevisitor> revisitors = repository.SearchJson(criteria);
String filename = new RevisitorSaver().Save(revisitors);
//
Byte[] content = File.ReadAllBytes(filename);
MemoryStream memStream = new MemoryStream(content);
//
HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.OK);
response.Content = new StreamContent(memStream);
response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment");
response.Content.Headers.ContentDisposition.FileName = filename;
return response;
}
The RevisitorSaver takes a collection of database object and write to an excel file.
botem
0 Points
3 Posts
Error when create excel file - 'Exception from HRESULT: 0x800AC472'
Feb 23, 2013 05:24 AM|LINK
I'm using WEB API to build REST service. One feature is to export the records in database as excel file.
I tried to implement export API in the server side to create an excel file, and then transmit the excel file back to the client. Here the code snippet.
[HttpGet] [ActionName("Export")] public HttpResponseMessage Export() { RevisitorCriteria criteria = new RevisitorCriteria() {...}; // IEnumerable<JsonRevisitor> revisitors = repository.SearchJson(criteria); String filename = new RevisitorSaver().Save(revisitors); // Byte[] content = File.ReadAllBytes(filename); MemoryStream memStream = new MemoryStream(content); // HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.OK); response.Content = new StreamContent(memStream); response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream"); response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment"); response.Content.Headers.ContentDisposition.FileName = filename; return response; }The RevisitorSaver takes a collection of database object and write to an excel file.
public String Save(IEnumerable<JsonObject> objects) { Workbook workbook = ExcelWrapper.ExcelWrapper.Instance.CreateWorkbook(); Worksheet worksheet = workbook.Worksheets[1] as Worksheet; worksheet.Name = SheetName; // WriteHeaders(worksheet); // WriteObjects(worksheet, objects); // DirectoryInfo directory = new DirectoryInfo(HttpContext.Current.Server.MapPath(@"~\Temp")); if (Directory.Exists(directory.FullName) == false) Directory.CreateDirectory(directory.FullName); String filename = Path.Combine(directory.FullName, Guid.NewGuid().ToString() + ".xlsx"); workbook.SaveAs(filename); // ExcelWrapper.ExcelWrapper.Instance.Dispose(workbook); // return filename; }The ExcelWrapper is a shared library, I'm sure it works as it's widly used by other C# WinForm application.
I get an error 'Exception from HRESULT: 0x800AC472'?
Is anybody has the experience to create excel file (2010)?
Thanks
ignatandrei
All-Star
134521 Points
21576 Posts
Moderator
MVP
Re: Error when create excel file - 'Exception from HRESULT: 0x800AC472'
Feb 23, 2013 05:51 AM|LINK
Which line?