I have a problem about string format c # I have my code with more than 12 digits and the excel is shown as 2.34234E + 17

0

VISTA

<table id="t01">
    @{
        if (ViewBag.TipoReporte == 1)
        {
            <tr align="center" valign="middle">
                <th colspan="10"><h1>STOCK GENERAL</h1></th>
            </tr>
            <tr align="center" valign="middle">
                <th colspan="10"><h1>A LA FECHA  @ViewBag.fecha</h1></th>
            </tr>
            <tr>
                <th>Tipo</th>
                <th>Codigo</th>
                <th>Material</th>
                <th>Rubro</th>
                <th>UM</th>
                <th>Ingresos</th>
                <th>Salidas</th>
                <th>Stock</th>
                <th>Costo</th>
                <th>Valorizado</th>
            </tr>
            foreach (var item in ViewBag.DatosExcel)
            {
                <tr>
                    <td>@item.TipoAlmacen</td>
                    <td>@String.Format(item.codigo_interno.ToString())</td>
                    <td>@item.descripcion_material</td>
                    <td>@item.rubro</td>
                    <td>@item.unidadmaterial</td>
                    <td align="right">@String.Format("{0:0.00}", item.ingreso).Replace(",", ".")</td>
                    <td align="right">@String.Format("{0:0.00}", item.salida).Replace(",", ".")</td>
                    <td align="right">@String.Format("{0:0.00}", item.stock).Replace(",", ".")</td>
                    <td align="right">@String.Format("{0:0.00}", item.costo).Replace(",", ".")</td>
                    <td align="right">@String.Format("{0:0.00}", item.valorizado).Replace(",", ".")</td>
                </tr>
            }
        }
</table>

CONTROLLER

public ActionResult ExportarExcelStockAlmacen(string fecha, int Local = 0, int Almacen = 0, int LocalAlmacen = 0, int TipoReporte = 0)
        {

            ReportesAD oReportesAD = new ReportesAD();
            ReporteStockAlmacen record = new ReporteStockAlmacen();

            record.local = Convert.ToInt32(Local);
            record.almacen = Convert.ToInt32(Almacen);
            record.fechaEmisionDoc_GuiasCab = Convert.ToString(fecha);
            record.usuario = Convert.ToInt32(((Sesion)Session["Session_Usuario_Acceso"]).usuario.id_Usuario);
            //record.fechaemision = Convert.ToInt32(fecha);

            // por stock general
            if (TipoReporte == 1)
            {
                record.tiporeporte = Convert.ToInt32(1);
                ViewBag.DatosExcel = oReportesAD.BuscarPorStockGeneral(record);
            }
            // por stock almacen
            else if (TipoReporte == 3)
            {
                record.tiporeporte = Convert.ToInt32(2);
                ViewBag.DatosExcel = oReportesAD.BuscarPorStockAlmacenGeneral(record);
            }
            // Stock General detallado por Movimientos
            else if (TipoReporte == 2)
            {
                record.tiporeporte = Convert.ToInt32(1);
                ViewBag.DatosExcel = oReportesAD.BuscarPorStockGeneralMovimientos(record);
            }
            // Movimientos o Stock por Almacen por Movimientos
            else if (TipoReporte == 4)
            {
                record.tiporeporte = Convert.ToInt32(2);
                ViewBag.DatosExcel = oReportesAD.BuscarPorStockAlmacenMovimientos(record);
            }

            string archivo = "ReporteExcel_Almacen_" + DateTime.Now.ToString("yyyyMMdd_hhmmss") + ".xls";
            Response.AddHeader("content-disposition", "attachment; filename=" + archivo);
            Response.ContentType = "application/ms-excel";

            ViewBag.TipoReporte = TipoReporte;
            ViewBag.fecha = fecha;
            return View();
        }
    
asked by Naxin IA 27.02.2017 в 15:34
source

2 answers

0

The problem is not the code, the number is too large for it to enter the excel box, when the box is enlarged the number will appear perfectly. You could expand the box using css, it could work.

    
answered by 27.02.2017 в 21:11
0

Excel has that behavior by default. If what you want is to show a key (for example, since I do not understand in which data the problem is) then from the code do not treat it as a number, but as string (and in your database as char(12) if is a fixed number of characters or as varchar if it is not ..

Additionally, if you want to avoid this Excel behavior with the numbers, put a simple quote at the beginning of the text and you will recognize it as such:

foreach (var item in ViewBag.DatosExcel)
{
    <tr>
        <td>@item.TipoAlmacen</td>
        <td>'@item.codigo_interno</td> @* Suponiendo que debe ser aquí *@
        <td>@item.descripcion_material</td>
        <td>@item.rubro</td>
        <td>@item.unidadmaterial</td>
        <td align="right">@String.Format("'{0:0.00}", item.ingreso).Replace(",", ".")</td> @* Suponiendo que deba ir aquí *@
        <td align="right">@String.Format("{0:0.00}", item.salida).Replace(",", ".")</td>
        <td align="right">@String.Format("{0:0.00}", item.stock).Replace(",", ".")</td>
        <td align="right">@String.Format("{0:0.00}", item.costo).Replace(",", ".")</td>
        <td align="right">@String.Format("{0:0.00}", item.valorizado).Replace(",", ".")</td>
    </tr>
}

Keep in mind that this will show them as text, so forget the arithmetic operations.

    
answered by 02.03.2017 в 16:35