我这边将数据导出,会自动将0123 转换成123,也就是如果设置列为 文本格式?

解决方案 »

  1.   

     System.Reflection.Missing miss = System.Reflection.Missing.Value;
                Excel.ApplicationClass m_objExcel = new Microsoft.Office.Interop.Excel.ApplicationClass();
                m_objExcel.Visible = true;
                Excel.Workbooks m_objBooks = (Excel.Workbooks)m_objExcel.Workbooks;
                Excel.Workbook m_objBook = (Excel.Workbook)(m_objBooks.Add(miss));
                Excel.Worksheet m_ojbSheet = (Excel.Worksheet)m_objBook.ActiveSheet;
               // ((Excel.Range)(m_ojbSheet.Rows["1", Type.Missing])).Height = 10;            m_ojbSheet.get_Range("A1", "N1").Merge(true);
                m_ojbSheet.get_Range("A1", "N1").Value2 = "厦门广播数字电视工程公司" + type + "收费情况表";
                m_ojbSheet.get_Range("A1", "N1").Font.Size = 18;            m_ojbSheet.Cells[2,1] = "序号";
                m_ojbSheet.Cells[2,2] = "工单号";
                m_ojbSheet.Cells[2,3] = "客户编号";
                m_ojbSheet.Cells[2,4] = "用户名";
                m_ojbSheet.Cells[2,5] = "地址";
                m_ojbSheet.Cells[2,6] = "MAC地址";
                m_ojbSheet.Cells[2,7] = "收据号";
                m_ojbSheet.Cells[2,8] = "实收金额";
                m_ojbSheet.Cells[2,9] = "安装人员";
                m_ojbSheet.Cells[2,10] = "安装日期";
                m_ojbSheet.Cells[2,11] = "申请日期";
                m_ojbSheet.Cells[2,12] = "缴款日期";
                m_ojbSheet.Cells[2,13] = "备注";            ((Excel.Range)(m_ojbSheet.Columns[10,Type.Missing])).ColumnWidth = ((Excel.Range)(m_ojbSheet.Columns[11,Type.Missing])).ColumnWidth = ((Excel.Range)(m_ojbSheet.Columns[12,Type.Missing])).ColumnWidth = 18;
                ((Excel.Range)(m_ojbSheet.Columns[5,Type.Missing])).ColumnWidth = 20;            int rowIndex = 3;
                foreach (DataRow dr in dt.Rows)
                {
                    m_ojbSheet.Cells[rowIndex, 1] = Convert.ToString(rowIndex - 2);
                    m_ojbSheet.Cells[rowIndex, 2] = dr["工单号"].ToString();
                    m_ojbSheet.Cells[rowIndex, 3] = dr["客户编号"].ToString();
                    m_ojbSheet.Cells[rowIndex, 4] = dr["用户名"].ToString();
                    m_ojbSheet.Cells[rowIndex, 5] = dr["地址"].ToString();
                    m_ojbSheet.Cells[rowIndex, 6] = dr["MAC地址"].ToString();
                    m_ojbSheet.Cells[rowIndex, 7] = dr["收据号"].ToString();
                    m_ojbSheet.Cells[rowIndex, 8] = dr["实收金额"].ToString();
                    ...
                    rowIndex++;
                }            string fileName = DateTime.Now.ToString().Replace(":", "").Replace("-", "").Replace(" ", "") + ".xls";
                if (!Directory.Exists("C:\\ExcelsTemp"))
                {
                    Directory.CreateDirectory("C:\\ExcelsTemp");
                }
                if (File.Exists("C:\\ExcelsTemp\\" + fileName))
                {
                    File.Delete("C:\\ExcelsTemp\\" + fileName);
                }
                try
                {
                    m_objBook.SaveAs("C:\\ExcelsTemp\\" + fileName, miss, miss, miss, miss, miss,
                                   Excel.XlSaveAsAccessMode.xlNoChange, miss, miss, miss, miss, miss); //保存到指定文件夹  
                }
                catch
                {
                }
                finally
                {
                    m_objBook.Close(false, miss, miss);
                    m_objBooks.Close();
                    m_objExcel.Quit();
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(m_ojbSheet);
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(m_objBook);
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(m_objBooks);
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(m_objExcel);
                    GC.Collect();
                }            object missing = System.Reflection.Missing.Value;            Excel.ApplicationClass excelApp = new Excel.ApplicationClass();
                Excel.Workbook workbook = excelApp.Workbooks.Open("C:\\ExcelsTemp\\" + fileName, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing);
                excelApp.Visible = true;