您好,欢迎来到[编程问答]网站首页   源码下载   电子书籍   软件下载   专题
当前位置:首页 >> 编程问答 >> .NET >> winform里,将值导入EXCEL中一个sheet,再导第二个sheet 提示错误

winform里,将值导入EXCEL中一个sheet,再导第二个sheet 提示错误

来源:网络整理     时间:2016/6/25 5:50:48     关键词:

关于网友提出的“ winform里,将值导入EXCEL中一个sheet,再导第二个sheet 提示错误”问题疑问,本网通过在网上对“ winform里,将值导入EXCEL中一个sheet,再导第二个sheet 提示错误”有关的相关答案进行了整理,供用户进行参考,详细问题解答如下:

问题: winform里,将值导入EXCEL中一个sheet,再导第二个sheet 提示错误
描述:

     如题。。     代码如下。。。 =====================================================          //ProgressBar pb = new ProgressBar();             object oMissing = System.Reflection.Missing.Value;             Excel.ApplicationClass xlApp = new Excel.ApplicationClass();<>             try             {<>                 //打开EXCEL文件                  Excel.Workbook xlWorkbook = xlApp.Workbooks.Open(filepath, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing);                 // Excel.Workbook xlWorkbook=xlApp.Workbooks.只有Open属性,没有Write属性                  // Excel.Worksheet xlWorksheet;                 //循环所有DataTable 
                for (int i = 0; i < ds.Tables.Count; i++)
                {                     //添加入一个新的Sheel页 
                    //  xlWorksheet = (Excel.Worksheet)xlWorkbook.Worksheets.Add(oMissing, oMissing, 1, oMissing);                     ////  以TableName作为新加的sheel页名 
                    //  xlWorksheet.Name ="sadasdas"; Excel.Worksheet xlWorksheet = (Excel.Worksheet)xlApp.ActiveSheet;                     xlWorksheet.Name = SheetName;                                        //xlWorksheet = (Excel.Worksheet)xlWorkbook.Sheets["ASQ"];                     //xlWorksheet.Activate() ;                     progressBar1.Visible = true;
                    progressBar1.Maximum =  ds.Tables[i].Rows.Count * ds.Tables[i].Columns.Count;                     progressBar1.Minimum = 0;                     int step = progressBar1.Maximum / ds.Tables[i].Rows.Count;                     progressBar1.Step = step;

                    progressBar1.Value = progressBar1.Maximum - step *ds.Tables[i].Rows.Count;                                          //取出这个DataTable中的所有值,暂时存于stringBuffer中                      string stringBuffer = "";                                        for (int j = 0; j < ds.Tables[i].Rows.Count; j++)                     {                         progressBar1.Value = progressBar1.Value + step;                         for (int k=0; k < ds.Tables[i].Columns.Count; k++)                         {                             cout3 = j; cout4 = k;                                                         stringBuffer += ds.Tables[i].Rows[j][k].ToString();                             if (k < ds.Tables[i].Columns.Count - 1)
                                stringBuffer += "\t";
                        }                         stringBuffer += "\n";                                         }                     progressBar1.Value = 0;                     //利用系统剪贴板                      System.Windows.Forms.Clipboard.SetDataObject("");                     //将stringBuffer放入剪贴板                      System.Windows.Forms.Clipboard.SetDataObject(stringBuffer);                     //选中这个sheel页中的第一个单元格                      ((Excel.Range)xlWorksheet.Cells[2, 1]).Select();                     //粘贴                      xlWorksheet.Paste(oMissing, oMissing);                     //清空系统剪贴板                      System.Windows.Forms.Clipboard.SetDataObject("");                                   }
/>                 //保存并关闭这个工作薄                  xlWorkbook.Close(Excel.XlSaveAction.xlSaveChanges, oMissing, oMissing);                 System.Runtime.InteropServices.Marshal.ReleaseComObject(xlWorkbook);                 xlWorkbook = null;                               progressBar1.Visible = false;             }             catch (Exception ex)             {                 MessageBox.Show(ex.Message);             }             finally             {                 //释放...                  xlApp.Quit();                 System.Runtime.InteropServices.Marshal.ReleaseComObject(xlApp);                 xlApp = null;                                  GC.Collect();                // GC.WaitForPendingFinalizers();             }
===========================    带颜色部分,在我导入第一次数据,sheetName也赋值成功。随即导入的EXCEL名字和内容都是我要的。当我连着执行这方法两次。第一次成功,第二次就会提示。                         " 所要重命名的工作表不能与另一个工作表,被引用的对象程序库或被 Visual Basic 引用的工作簿重名。"       是不是我之前第一张表导入成功后,没彻底关闭?还是我选择的活动表仍是之前那个?               


以上介绍了“ winform里,将值导入EXCEL中一个sheet,再导第二个sheet 提示错误”的问题解答,希望对有需要的网友有所帮助。
本文网址链接:http://www.codes51.com/itwd/2053411.html

相关图片

相关文章