asp.net英文操作系统下导出EXCEL,不会乱码

        <asp:GridView ID="grvData" runat="server" CellPadding="4" ForeColor="#333333" GridLines="None" AutoGenerateColumns="False" PageSize="10000" OnRowDataBound="grvData_RowDataBound">
            <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
            <Columns>
                <asp:TemplateField HeaderText ="导出">
                    <ItemTemplate>
                        <asp:LinkButton ID="LinkButton1" OnClick="LinkButton1_Click" CommandName="GetText" CommandArgument='<%#Eval("projectno") %>' Text="导出Excel" runat="server"></asp:LinkButton>
                    </ItemTemplate>
                </asp:TemplateField>


-------------------


using Microsoft.Office.Interop.Excel;
using System.IO;
using System.Text;


    protected void LinkButton1_Click(object sender, EventArgs e)
    {
        if (ViewState["ds"] == null && ((DataSet)ViewState["ds"]).Tables[0].Rows.Count == 0)
            return;


        Microsoft.Office.Interop.Excel.Application xlsApp = new Microsoft.Office.Interop.Excel.Application();
        if (xlsApp == null)
        {
            Response.Write("<script>alert('请检查本机是否安装Office Excel软件!');</script>");
            return;
        }
        try
        {
            System.Globalization.CultureInfo CurrentCI = System.Threading.Thread.CurrentThread.CurrentCulture;
            System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("zh-Cn");  //"en-US" 
            Microsoft.Office.Interop.Excel.Workbooks workbooks = xlsApp.Workbooks;
            Microsoft.Office.Interop.Excel.Workbook workbook = workbooks.Add();


            Microsoft.Office.Interop.Excel.Worksheet worksheet1 = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Sheets.Add(Type.Missing, Type.Missing, Type.Missing, Type.Missing);
            Microsoft.Office.Interop.Excel.Worksheet worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Sheets.Add(Type.Missing, Type.Missing, Type.Missing, Type.Missing);


            Microsoft.Office.Interop.Excel.Range range1;
            Microsoft.Office.Interop.Excel.Range range;


            long rowscount = 0;
            worksheet.Name = "NMR file_consolidation"; //报表-工程;


            System.Data.DataSet ds = new System.Data.DataSet();
            string projectno = ((LinkButton)sender).CommandArgument.ToString();
            NMRBLL nmrBll = new NMRBLL();
            ds = nmrBll.nmr_to_excel2(projectno);
            rowscount = ds.Tables[0].Rows.Count;


            worksheet.Cells[1, 1] = "零件编号";
            range = worksheet.get_Range(worksheet.Cells[1, 1], worksheet.Cells[1, 1]);
            range.ColumnWidth = 20;
            worksheet.Cells[1, 2] = "零件名称";
            range = worksheet.get_Range(worksheet.Cells[1, 2], worksheet.Cells[1, 2]);
            range.ColumnWidth = 20;
            worksheet.Cells[1, 3] = "客户编号";
            range = worksheet.get_Range(worksheet.Cells[1, 3], worksheet.Cells[1, 3]);
            range.ColumnWidth = 13;
            worksheet.Cells[1, 4] = "客户描述";
            range = worksheet.get_Range(worksheet.Cells[1, 4], worksheet.Cells[1, 4]);
            range.ColumnWidth = 10;
            worksheet.Cells[1, 5] = "材质";
            range = worksheet.get_Range(worksheet.Cells[1, 5], worksheet.Cells[1, 5]);
            range.ColumnWidth = 10;
            worksheet.Cells[1, 6] = "备注";
            range = worksheet.get_Range(worksheet.Cells[1, 6], worksheet.Cells[1, 6]);
            range.ColumnWidth = 10;
            worksheet.Cells[1, 7] = "零件类别";
            range = worksheet.get_Range(worksheet.Cells[1, 7], worksheet.Cells[1, 7]);
            range.ColumnWidth = 13;
            worksheet.Cells[1, 8] = "是否起模";
            range = worksheet.get_Range(worksheet.Cells[1, 8], worksheet.Cells[1, 8]);
            range.ColumnWidth = 10;
            worksheet.Cells[1, 9] = "负责人";
            range = worksheet.get_Range(worksheet.Cells[1, 9], worksheet.Cells[1, 9]);
            range.ColumnWidth = 8;
            worksheet.Cells[1, 10] = "最新状态";
            range = worksheet.get_Range(worksheet.Cells[1, 10], worksheet.Cells[1, 10]);
            range.ColumnWidth = 20;
            worksheet.Cells[1, 11] = "采购报价时间";
            range = worksheet.get_Range(worksheet.Cells[1, 11], worksheet.Cells[1, 11]);
            range.ColumnWidth = 16;
            worksheet.Cells[1, 12] = "确认报价时间";
            range = worksheet.get_Range(worksheet.Cells[1, 12], worksheet.Cells[1, 12]);
            range.ColumnWidth = 18;
            worksheet.Cells[1, 13] = "最后一次送样时间";
            range = worksheet.get_Range(worksheet.Cells[1, 13], worksheet.Cells[1, 13]);
            range.ColumnWidth = 20;
            worksheet.Cells[1, 14] = "工程最后一次审批时间";
            range = worksheet.get_Range(worksheet.Cells[1, 14], worksheet.Cells[1, 14]);
            range.ColumnWidth = 25;
            worksheet.Cells[1, 15] = "QA发布测试报告时间";
            range = worksheet.get_Range(worksheet.Cells[1, 15], worksheet.Cells[1, 15]);
            range.ColumnWidth = 25;
            worksheet.Cells[1, 16] = "AE确认样板时间";
            range = worksheet.get_Range(worksheet.Cells[1, 16], worksheet.Cells[1, 16]);
            range.ColumnWidth = 18;


            worksheet.Cells[1, 17] = "每台车用量";
            range = worksheet.get_Range(worksheet.Cells[1, 17], worksheet.Cells[1, 17]);
            range.ColumnWidth = 18;
            worksheet.Cells[1, 18] = "CAR数量需求";
            range = worksheet.get_Range(worksheet.Cells[1, 18], worksheet.Cells[1, 18]);
            range.ColumnWidth = 18;
            worksheet.Cells[1, 19] = "采购实送数量";
            range = worksheet.get_Range(worksheet.Cells[1, 19], worksheet.Cells[1, 19]);
            range.ColumnWidth = 18;
            worksheet.Cells[1, 20] = "调试夹具数量";
            range = worksheet.get_Range(worksheet.Cells[1, 20], worksheet.Cells[1, 20]);
            range.ColumnWidth = 18;
            worksheet.Cells[1, 21] = "EP数量";
            range = worksheet.get_Range(worksheet.Cells[1, 21], worksheet.Cells[1, 21]);
            range.ColumnWidth = 18;
            worksheet.Cells[1, 22] = "供应商免费送板数量";
            range = worksheet.get_Range(worksheet.Cells[1, 22], worksheet.Cells[1, 22]);
            range.ColumnWidth = 20;
            worksheet.Cells[1, 23] = "零件欠缺数量";
            range = worksheet.get_Range(worksheet.Cells[1, 23], worksheet.Cells[1, 23]);
            range.ColumnWidth = 18;


            worksheet.Cells[1, 24] = "价格(RMB含税)";
            range = worksheet.get_Range(worksheet.Cells[1, 24], worksheet.Cells[1, 24]);
            range.ColumnWidth = 18;
            worksheet.Cells[1, 25] = "首批是否需客供";
            range = worksheet.get_Range(worksheet.Cells[1, 25], worksheet.Cells[1, 25]);
            range.ColumnWidth = 18;
            worksheet.Cells[1, 26] = "备注";
            range = worksheet.get_Range(worksheet.Cells[1, 26], worksheet.Cells[1, 26]);
            range.ColumnWidth = 10;
            worksheet.Cells[1, 27] = "供应商";
            range = worksheet.get_Range(worksheet.Cells[1, 27], worksheet.Cells[1, 27]);
            range.ColumnWidth = 15;
            worksheet.Cells[1, 28] = "AE备注";
            range = worksheet.get_Range(worksheet.Cells[1, 28], worksheet.Cells[1, 28]);
            range.ColumnWidth = 10;


            range = worksheet.get_Range(worksheet.Cells[1, 1], worksheet.Cells[1, 28]);
            range.Interior.ColorIndex = 50;
            range.Font.Bold = true;


            string nmr_status = "0";


            for (int j = 0; j < rowscount; j++)
            {
                worksheet.Cells[j + 2, 1] = ds.Tables[0].Rows[j]["partno"].ToString();
                worksheet.Cells[j + 2, 2] = ds.Tables[0].Rows[j]["partname"].ToString();
                worksheet.Cells[j + 2, 3] = ds.Tables[0].Rows[j]["pridepartno"].ToString();
                worksheet.Cells[j + 2, 4] = ds.Tables[0].Rows[j]["pridedescription"].ToString();
                worksheet.Cells[j + 2, 5] = ds.Tables[0].Rows[j]["materialtype"].ToString();
                worksheet.Cells[j + 2, 6] = ds.Tables[0].Rows[j]["remark"].ToString();
                worksheet.Cells[j + 2, 7] = ds.Tables[0].Rows[j]["parttype"].ToString();
                worksheet.Cells[j + 2, 8] = ds.Tables[0].Rows[j]["isqm"].ToString();
                worksheet.Cells[j + 2, 9] = ds.Tables[0].Rows[j]["fzr"].ToString();


                nmr_status = ds.Tables[0].Rows[j]["nmr_status"].ToString().Trim();
                if (nmr_status == "1")
                {
                    worksheet.Cells[j + 2, 10] = "工程主管审批中";
                }
                if (nmr_status == "2")
                {
                    worksheet.Cells[j + 2, 10] = "工程主管审批通过";
                }
                if (nmr_status == "2.1")
                {
                    worksheet.Cells[j + 2, 10] = "工程主管审批不通过";
                }
                if (nmr_status == "3")
                {
                    worksheet.Cells[j + 2, 10] = "采购接收报价";
                }
                if (nmr_status == "3.1")
                {
                    worksheet.Cells[j + 2, 10] = "采购不接收报价";
                }
                if (nmr_status == "4")
                {
                    worksheet.Cells[j + 2, 10] = "采购报价完成";
                }
                if (nmr_status == "5")
                {
                    worksheet.Cells[j + 2, 10] = "AE同意采购报价";
                }
                if (nmr_status == "5.1")
                {
                    worksheet.Cells[j + 2, 10] = "AE不同意采购报价";
                }
                if (nmr_status == "6")
                {
                    worksheet.Cells[j + 2, 10] = "采购已接收样板";
                }
                if (nmr_status == "7")
                {
                    worksheet.Cells[j + 2, 10] = "工程通过样板审批";
                }
                if (nmr_status == "7.1")
                {
                    worksheet.Cells[j + 2, 10] = "工程不通过样板审批";
                }
                if (nmr_status == "8")
                {
                    worksheet.Cells[j + 2, 10] = "QA已检测样板";
                }
                if (nmr_status == "9")
                {
                    worksheet.Cells[j + 2, 10] = "AE通过审批样板";
                }
                if (nmr_status == "9.1")
                {
                    worksheet.Cells[j + 2, 10] = "AE不通过审批样板";
                }


                worksheet.Cells[j + 2, 11] = ds.Tables[0].Rows[j]["pur_approveddate"].ToString();
                worksheet.Cells[j + 2, 12] = ds.Tables[0].Rows[j]["ae_quotation_date"].ToString();
                worksheet.Cells[j + 2, 13] = ds.Tables[0].Rows[j]["vi_upload_date"].ToString();
                worksheet.Cells[j + 2, 14] = ds.Tables[0].Rows[j]["eng_approval_date"].ToString();
                worksheet.Cells[j + 2, 15] = ds.Tables[0].Rows[j]["qa_approval_date"].ToString();
                worksheet.Cells[j + 2, 16] = ds.Tables[0].Rows[j]["ae_approval_date"].ToString();


                worksheet.Cells[j + 2, 17] = ds.Tables[0].Rows[j]["qty_eng"].ToString();
                worksheet.Cells[j + 2, 18] = ds.Tables[0].Rows[j]["qty_car_eng"].ToString();
                worksheet.Cells[j + 2, 19] = ds.Tables[0].Rows[j]["qty_pur"].ToString();
                worksheet.Cells[j + 2, 20] = ds.Tables[0].Rows[j]["qty_pe"].ToString();
                worksheet.Cells[j + 2, 21] = ds.Tables[0].Rows[j]["qty_ep"].ToString();
                worksheet.Cells[j + 2, 22] = ds.Tables[0].Rows[j]["qty_vender"].ToString();
                worksheet.Cells[j + 2, 23] = ds.Tables[0].Rows[j]["qty_total"].ToString();


                worksheet.Cells[j + 2, 24] = ds.Tables[0].Rows[j]["unitprice"].ToString();
                worksheet.Cells[j + 2, 25] = ds.Tables[0].Rows[j]["unitprice_is"].ToString();
                worksheet.Cells[j + 2, 26] = ds.Tables[0].Rows[j]["unitprice_remark"].ToString();
                worksheet.Cells[j + 2, 27] = ds.Tables[0].Rows[j]["unitprice_vender"].ToString();
                worksheet.Cells[j + 2, 28] = ds.Tables[0].Rows[j]["ae_remark"].ToString();


                if (j % 2 == 1)
                {
                    range = worksheet.get_Range(worksheet.Cells[j + 2, 1], worksheet.Cells[j + 2, 28]);
                    range.Interior.ColorIndex = 35;
                }
            }
            range = worksheet.get_Range(worksheet.Cells[1, 1], worksheet.Cells[rowscount + 1, 28]);
            range.Borders.LineStyle = XlLineStyle.xlContinuous;
            range.Columns.WrapText = true;


            worksheet1.Activate();
            range1 = worksheet1.get_Range(worksheet1.Cells[2, 4], worksheet1.Cells[2, 4]);
            range1.Select();
            xlsApp.ActiveWindow.FreezePanes = true;


            worksheet.Activate();
            range = worksheet.get_Range(worksheet.Cells[2, 4], worksheet.Cells[2, 4]);
            range.Select();
            xlsApp.ActiveWindow.FreezePanes = true;


            xlsApp.DisplayAlerts = false;
            xlsApp.Visible = false;
            workbook.Saved = true;


            string FileName = Server.MapPath("~/Files/" + "NMR file_consolidation" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".xlsx");


            workbook.SaveCopyAs(FileName);


            FileDownload(FileName);


            System.Runtime.InteropServices.Marshal.ReleaseComObject(worksheet);
            worksheet = null;
            System.Runtime.InteropServices.Marshal.ReleaseComObject(worksheet1);
            worksheet1 = null;
            System.Runtime.InteropServices.Marshal.ReleaseComObject(workbook);
            workbook = null;
            workbooks.Close();
            System.Runtime.InteropServices.Marshal.ReleaseComObject(workbooks);
            workbooks = null;
            xlsApp.Quit();
            System.Runtime.InteropServices.Marshal.ReleaseComObject(xlsApp);
            xlsApp = null;
            GC.Collect();
        }
        catch
        {
            Response.Write("<script>alert('生成Office Excel失败!');</script>");
        }
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值