菜鸟copy的c#代码执行到SaveCopyAs()时出现未处理comexception的错误
就是执行到最后的时候---这句xlBook.SaveCopyAs(strFileName);出现未处理comexception的错误,找不到1.xls文件,但是我已经在c...
就是执行到最后的时候--- 这句xlBook.SaveCopyAs(strFileName);出现未处理comexception的错误,找不到1.xls文件,但是我已经在c盘建了一个1.xls,执行到最后1.xls被删除了。。。。。。
string commandString = "SELECT DepartmentName,EmployeeCode,ChineseName,Validate FROM EmployeeMsg Where Validate = dateadd(month,-3,getdate())";
SqlDataAdapter dataAdapter = new SqlDataAdapter(commandString, connStr);
DataSet ds = new DataSet();
dataAdapter.Fill(ds,"员工试用期到期表");
System.Data.DataTable table = ds.Tables["员工试用期到期表"];
DataTabletoExcel(table, "C:\\\\1.xls");
conn.Close();
}
//数据导出
private void DataTabletoExcel(System.Data.DataTable tmpDataTable, string strFileName)
{
if (tmpDataTable == null)
{
return;
}
int rowNum = tmpDataTable.Rows.Count;
int columnNum = tmpDataTable.Columns.Count;
int rowIndex = 1;
int columnIndex = 0;
Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.ApplicationClass();
xlApp.DefaultFilePath = "C:\\";
xlApp.DisplayAlerts = true;
xlApp.SheetsInNewWorkbook = 1;
Microsoft.Office.Interop.Excel.Workbook xlBook = xlApp.Workbooks.Add(true);
//将DataTable的列名导入Excel表第一行
foreach (DataColumn dc in tmpDataTable.Columns)
{
columnIndex++;
xlApp.Cells[rowIndex, columnIndex] = dc.ColumnName;
}
//将DataTable中的数据导入Excel中
for (int i = 0; i < rowNum; i++)
{
rowIndex++;
columnIndex = 0;
for (int j = 0; j < columnNum; j++)
{
columnIndex++;
xlApp.Cells[rowIndex, columnIndex] = tmpDataTable.Rows[i][j].ToString();
}
}
xlBook.SaveCopyAs(strFileName);
}
} 展开
string commandString = "SELECT DepartmentName,EmployeeCode,ChineseName,Validate FROM EmployeeMsg Where Validate = dateadd(month,-3,getdate())";
SqlDataAdapter dataAdapter = new SqlDataAdapter(commandString, connStr);
DataSet ds = new DataSet();
dataAdapter.Fill(ds,"员工试用期到期表");
System.Data.DataTable table = ds.Tables["员工试用期到期表"];
DataTabletoExcel(table, "C:\\\\1.xls");
conn.Close();
}
//数据导出
private void DataTabletoExcel(System.Data.DataTable tmpDataTable, string strFileName)
{
if (tmpDataTable == null)
{
return;
}
int rowNum = tmpDataTable.Rows.Count;
int columnNum = tmpDataTable.Columns.Count;
int rowIndex = 1;
int columnIndex = 0;
Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.ApplicationClass();
xlApp.DefaultFilePath = "C:\\";
xlApp.DisplayAlerts = true;
xlApp.SheetsInNewWorkbook = 1;
Microsoft.Office.Interop.Excel.Workbook xlBook = xlApp.Workbooks.Add(true);
//将DataTable的列名导入Excel表第一行
foreach (DataColumn dc in tmpDataTable.Columns)
{
columnIndex++;
xlApp.Cells[rowIndex, columnIndex] = dc.ColumnName;
}
//将DataTable中的数据导入Excel中
for (int i = 0; i < rowNum; i++)
{
rowIndex++;
columnIndex = 0;
for (int j = 0; j < columnNum; j++)
{
columnIndex++;
xlApp.Cells[rowIndex, columnIndex] = tmpDataTable.Rows[i][j].ToString();
}
}
xlBook.SaveCopyAs(strFileName);
}
} 展开
展开全部
我对EXCEL操作也不熟悉,以下供参考:
创建EXCEL时用这句:
Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();
Microsoft.Office.Interop.Excel.Workbooks workbooks = xlApp.Workbooks;
Microsoft.Office.Interop.Excel.Workbook workbook = workbooks.Add(Microsoft.Office.Interop.Excel.XlWBATemplate.xlWBATWorksheet);
Microsoft.Office.Interop.Excel.Worksheet worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Worksheets[1];//取得sheet1
写入EXCEL时:
worksheet.Cells[rowIndex, columnIndex] =“内容”;
保存EXCEL时:
workbook.Saved = true;
workbook.SaveCopyAs(“C:\\我的文件.xsl”);
你可以据此修改测试下看是否能运行。
创建EXCEL时用这句:
Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();
Microsoft.Office.Interop.Excel.Workbooks workbooks = xlApp.Workbooks;
Microsoft.Office.Interop.Excel.Workbook workbook = workbooks.Add(Microsoft.Office.Interop.Excel.XlWBATemplate.xlWBATWorksheet);
Microsoft.Office.Interop.Excel.Worksheet worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Worksheets[1];//取得sheet1
写入EXCEL时:
worksheet.Cells[rowIndex, columnIndex] =“内容”;
保存EXCEL时:
workbook.Saved = true;
workbook.SaveCopyAs(“C:\\我的文件.xsl”);
你可以据此修改测试下看是否能运行。
更多追问追答
追问
改了,然后把第一行列名行的那段语句取消后,现在程序运行出来的表是空白的
追答
表是空白的话,可能是EXCEL创建出来没保存上,也可能是数据没写入EXCEL里。
可以打断点调试,看在哪儿没数据了。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询