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);
// xlWorksheet = (Excel.Worksheet)xlWorkbook.Sheets["ASQ"];
//以TableName作为新加的sheel页名
xlWorksheet.Name ="sadasdas"; //取出这个DataTable中的所有值,暂时存于stringBuffer中
string stringBuffer = "";
// for(int m=0;m<ds.Tab
for (int j = 0; j < ds.Tables[i].Rows.Count; j++)
{
for (int k = 0; k < ds.Tables[i].Columns.Count; k++)
{
stringBuffer += ds.Tables[i].Rows[j][k].ToString();
if (k < ds.Tables[i].Columns.Count - 1)
stringBuffer += "\t";
}
stringBuffer += "\n";
}
//利用系统剪贴板
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;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
//释放...
xlApp.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(xlApp);
xlApp = null;
GC.Collect();
}-------------------------------------------- 红色部分是不知道怎么修改的地方,一改老是出现问题。 求知道怎么修改的朋友给个提示~,谢谢
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);
// xlWorksheet = (Excel.Worksheet)xlWorkbook.Sheets["ASQ"];
//以TableName作为新加的sheel页名
xlWorksheet.Name ="sadasdas"; //取出这个DataTable中的所有值,暂时存于stringBuffer中
string stringBuffer = "";
// for(int m=0;m<ds.Tab
for (int j = 0; j < ds.Tables[i].Rows.Count; j++)
{
for (int k = 0; k < ds.Tables[i].Columns.Count; k++)
{
stringBuffer += ds.Tables[i].Rows[j][k].ToString();
if (k < ds.Tables[i].Columns.Count - 1)
stringBuffer += "\t";
}
stringBuffer += "\n";
}
//利用系统剪贴板
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;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
//释放...
xlApp.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(xlApp);
xlApp = null;
GC.Collect();
}-------------------------------------------- 红色部分是不知道怎么修改的地方,一改老是出现问题。 求知道怎么修改的朋友给个提示~,谢谢
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货