问题描述
datetime字段被导出为数字,但是如果我将单元格更改为Excel中的datetime类型,则它将获得正确的值。 除日期格式外,所有服务器导出功能均可用。 由于我正在执行服务器导出,因此我不能依靠https://docs.telerik.com/kendo-ui/knowledge-base/cell-format
这样的客户端解决方案控制器:
library(shinyjs)
library(openxlsx)
library(DT)
library(shiny)
# create some example data to download
my_table <- data.frame(
Name = letters[1:4],Age = seq(20,26,2),Occupation = LETTERS[15:18],Income = c(50000,20000,30000,45000)
)
# add a totals row
my_table <- rbind(
my_table,data.frame(
Name = "Total",Age = NA_integer_,Occupation = "",Income = sum(my_table$Income)
)
)
# minimal Shiny UI
ui <- fluidRow(
column(
width = 12,align = "center",DT::dataTableOutput("table_out")
))
# minimal Shiny server
server <- function(input,output) {
output$table_out <- renderDataTable(server = FALSE,{DT::datatable(my_table,extensions="Buttons",options = list(dom = 'Bfrtip',initComplete = JS(
"function(settings,json) {","$(this.api().table().header()).css({'color': '#fff','background':'#dd4b39'});","}"),buttons = list("excel","pdf")
))})}
shinyApp(ui,server)
导出表单/按钮:
[HttpPost]
public FileStreamResult ExportServer([DataSourceRequest]DataSourceRequest request,string model,string data)
{
var columnsData = JsonConvert.DeserializeObject<IList<ExportColumnSettings>>(HttpUtility.UrlDecode(model));
dynamic options = JsonConvert.DeserializeObject(HttpUtility.UrlDecode(data));
SpreadDocumentFormat exportFormat = options.format.ToString() == "csv" ? exportFormat = SpreadDocumentFormat.Csv : exportFormat = SpreadDocumentFormat.Xlsx;
Action<ExportCellStyle> cellStyle = new Action<ExportCellStyle>(ChangeCellStyle);
Action<ExportRowStyle> rowStyle = new Action<ExportRowStyle>(ChangeRowStyle);
Action<ExportColumnStyle> columnStyle = new Action<ExportColumnStyle>(ChangeColumnStyle);
string fileName = string.Format("{0}.{1}",options.title,options.format);
string mimeType = Helpers.GetMimeType(exportFormat);
Stream exportStream = exportFormat == SpreadDocumentFormat.Xlsx ?
db.VWMapaCompleto.ToList().ToDataSourceResult(request).Data.ToXlsxStream(columnsData,(string)options.title.ToString(),cellStyleAction: cellStyle,rowStyleAction: rowStyle,columnStyleAction: columnStyle) :
db.VWMapaCompleto.ToList().ToDataSourceResult(request).Data.ToCsvStream(columnsData);
var fileStreamResult = new FileStreamResult(exportStream,mimeType);
fileStreamResult.FileDownloadName = fileName;
fileStreamResult.FileStream.Seek(0,SeekOrigin.Begin);
return fileStreamResult;
}
private void ChangeCellStyle(ExportCellStyle e)
{
bool isHeader = e.Row == 0;
SpreadCellFormat format = new SpreadCellFormat
{
ForeColor = isHeader ? SpreadThemableColor.Fromrgb(216,184,168) : SpreadThemableColor.Fromrgb(0,0),//IsItalic = true,//VerticalAlignment = SpreadVerticalAlignment.Center,WrapText = true,Fill = SpreadPatternFill.CreateSolidFill(isHeader ? new SpreadColor(50,54,58) : new SpreadColor(255,255,255))
};
e.Cell.SetFormat(format);
}
private void ChangeRowStyle(ExportRowStyle e)
{
e.Row.SetHeightInPixels(e.Index == 0 ? 30 : 30);
}
private void ChangeColumnStyle(ExportColumnStyle e)
{
double width = e.Name == "Product name" || e.Name == "Category Name" ? 250 : 100;
e.Column.SetWidthInPixels(width+50);
}
解决方法
我通过添加以下内容解决了该问题:
NumberFormat =“ yyyy / MM / dd h:mm”
format = new SpreadCellFormat
{
ForeColor = isHeader ? SpreadThemableColor.FromRgb(216,184,168) : SpreadThemableColor.FromRgb(0,0),//IsItalic = true,//VerticalAlignment = SpreadVerticalAlignment.Center,WrapText = true,Fill = SpreadPatternFill.CreateSolidFill(isHeader ? new SpreadColor(50,54,58) : new SpreadColor(255,255,255)),NumberFormat = "yyyy/MM/dd h:mm"
};