asp.net - How do I change the format of a specific column in EPPlus? -
i've used epplus download datatable website / database excel sheet , first picture result get. second picture be.
questions:
- how change format of timestamp "time"?
obviously title still string format.
- how make width of columns match longest word inside?
so 80% of message isn't hidden , have drag column out read entire message.
edit: forgot add code...
public actionresult exportdata() { datatable datatable = getdata(); using (excelpackage package = new excelpackage()) { var ws = package.workbook.worksheets.add("my sheet"); //true generates headers ws.cells["1:1"].style.font.bold = true; ws.cells["a1"].loadfromdatatable(datatable, true); ws.cells[ws.dimension.address].autofitcolumns(); var stream = new memorystream(); package.saveas(stream); string filename = "log.xlsx"; string contenttype = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"; stream.position = 0; return file(stream, contenttype, filename); } } public datatable getdata() { datatable dt = new datatable(); if (modelstate.isvalid) { using (sqlconnection conn = new sqlconnection(system.configuration.configurationmanager.connectionstrings["mysqlconnection"].connectionstring)) { using (sqlcommand comm = conn.createcommand()) { comm.parameters.addwithvalue("@val1", session["myid"]); comm.parameters.addwithvalue("@val2", "%" + session["mysearchstring"] + "%"); comm.commandtext = "select * dbo.log customerid = @val1 , message @val2"; try { conn.open(); dt.load(comm.executereader()); } catch (sqlexception e) { throw new exception(e.tostring()); } } } } return dt; }
just need set numberformat.format
string. this:
ws.column(2).style.numberformat.format = "hh:mm:ss";
if want customize actual there plenty of resource online http://www.ozgrid.com/excel/excel-custom-number-formats.htm. or can open in excel, set format custom
, experiment string.
Comments
Post a Comment