导出Excel编码未打开

导出Excel编码未打开

本文介绍了导出Excel编码未打开的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 Microsoft.Office.Interop.Excel.Application objApp = new Microsoft.Office.Interop.Excel.Application();
            Microsoft.Office.Interop.Excel.Workbooks objBooks = objApp.Workbooks;
            Microsoft.Office.Interop.Excel.Workbook objBook = objBooks.Add(Missing.Value);
            Microsoft.Office.Interop.Excel.Worksheet objSheet = null;

            Microsoft.Office.Interop.Excel.Range range = null;

            if (dt.Rows.Count > 0)
            {
                objSheet = (Microsoft.Office.Interop.Excel.Worksheet)objBook.Worksheets.Add(Missing.Value, Missing.Value, Missing.Value, Missing.Value);
                ((Microsoft.Office.Interop.Excel.Worksheet)objBook.Sheets[1]).Select(Missing.Value);
 int row = 0;

                objSheet.Cells[row + 1, 2] = "SocCode";
 objBook.Close(true, @"D:\DMR.xlsx", Missing.Value);
                objBooks.Close();

            }

I am Generating Excel File that i file created i want open through code any body help me.........

推荐答案

if (dataGridView1.Rows.Count > 0)
           {
               Microsoft.Office.Interop.Excel.ApplicationClass XcelApp = newMicrosoft.Office.Interop.Excel.ApplicationClass();
               XcelApp.Application.Workbooks.Add(Type.Missing);

               for (int i = 1; i < dataGridView1.Columns.Count + 1; i++)
               {
                   XcelApp.Cells[1, i] = dataGridView1.Columns[i - 1].HeaderText;
               }

               for (int i = 0; i < dataGridView1.Rows.Count; i++)
               {
                   for (int j = 0; j < dataGridView1.Columns.Count; j++)
                   {
                       XcelApp.Cells[i + 2, j + 1] = dataGridView1.Rows[i].Cells[j].Value.ToString();
                   }
               }
               XcelApp.Columns.AutoFit();
               XcelApp.Visible = true;
           }


这篇关于导出Excel编码未打开的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 21:11