簡體   English   中英

從Excel導出Excel列格式為十進制

[英]Format excel column to decimal doing export from c#

嗨,我正在使用以下方法將數據庫導出到excel

        Response.ClearContent();
        Response.Buffer = true;
        Response.AddHeader("content-disposition", "attachment; filename=" + FileName);
        Response.ContentType = "application/vnd.ms-excel";
        EnableViewState = false;
        Response.Write("<style> TABLE { border:dotted 1px #999; } TH { border:dotted 1px #D5D5D5; text-align:center } TD { border:dotted 1px #D5D5D5; } </style>");
        Response.Write("<table>");
        Response.Write("<tr>");          
        Response.Write("<th>Actual Estimated Price</th>");
        Response.Write("<th>Aprroved Estimated Price </th>");
        Response.Write("<th>Actual Price</th>");
        Response.Write("<th>Aprroved Actual Price </th>");
        Response.Write("<th>TransactionID </th>");            
        Response.Write("<th>Created On</th>");
        Response.Write("</tr>");
        foreach (DataRow dr in dt.Rows)
        {
            Response.Write("<tr>");
            Response.Write("<td>");
            Response.Write(String.Format("{0:0.0#}", dr["EstimatedPriceTotal"].ToString()));
            Response.Write("</td>");
            Response.Write("<td>");
            Response.Write(String.Format("{0:0.0#}", dr["ApprovedEstimatedPriceTotal"].ToString()));
            Response.Write("</td>");
            Response.Write("<td>");
            Response.Write(String.Format("{0:0.0#}", dr["ActualPriceTotal"].ToString()));
            Response.Write("</td>");
            Response.Write("<td>");
            Response.Write(String.Format("{0:0.0#}", dr["ApprovedActualPriceTotal"].ToString()));
            Response.Write("</td>");
            Response.Write("<td>");
            Response.Write(dr["TransactionID"].ToString());
            Response.Write("</td>"); 
            Response.Write("<td>");    
            Response.Write(Convert.ToDateTime(dr["CreatedOn"].ToString()));
            Response.Write("</td>");
            Response.Write("</tr>");
        }
        Response.Write("</table>");
        Response.End();

但我無法以excel格式導出excel中的實際估計價格,批准的估計價格

該值顯示為5,而不是顯示5.00

如何從C#端將Excel的某些列格式化為十進制格式

更新資料

如何在EPPPlus中合並列標題合並

在此處輸入圖片說明

我想要兩個標題名稱

CustomerName
Mitesh Jain

這是一種完整的方法。 只需發送一個DataTable和一個文件名,其余的工作就可以完成。 該代碼段還將使標題行顯示為灰色,並帶有粗體文本,並將自動適合各列。

using OfficeOpenXml;
using OfficeOpenXml.Style;

public void ExportToExcel(DataTable dt, string FileName)
{
    //create a new byte array       
    byte[] bin;

    //create a new excel document
    using (ExcelPackage excelPackage = new ExcelPackage())
    {
        //create a new worksheet
        ExcelWorksheet ws = excelPackage.Workbook.Worksheets.Add(FileName);

        //add the contents of the datatable to the excel file
        ws.Cells["A1"].LoadFromDataTable(dt, true);

        //auto fix the columns
        ws.Cells[ws.Dimension.Address].AutoFitColumns();

        //loop all the columns
        for (int col = 1; col <= ws.Dimension.End.Column; col++)
        {
            //make all columns just a bit wider, it would sometimes not fit
            ws.Column(col).Width = ws.Column(col).Width + 1;

            var cell = ws.Cells[1, col];

            //make the text bold
            cell.Style.Font.Bold = true;

            //make the background of the cell gray
            var fill = cell.Style.Fill;
            fill.PatternType = ExcelFillStyle.Solid;
            fill.BackgroundColor.SetColor(ColorTranslator.FromHtml("#BFBFBF"));

            //make the header text upper case
            cell.Value = ((string)cell.Value).ToUpper();
        }

        //convert the excel package to a byte array
        bin = excelPackage.GetAsByteArray();
    }

    //clear the buffer stream
    Response.ClearHeaders();
    Response.Clear();
    Response.Buffer = true;

    //set the correct contenttype
    Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";

    //set the correct length of the data being send
    Response.AddHeader("content-length", bin.Length.ToString());

    //set the filename for the excel package
    Response.AddHeader("content-disposition", "attachment; filename=\"" + FileName + ".xlsx\"");

    //send the byte array to the browser
    Response.OutputStream.Write(bin, 0, bin.Length);

    //cleanup
    Response.Flush();
    HttpContext.Current.ApplicationInstance.CompleteRequest();
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM