下面是我的代码:请教高手指点下 我错误的地方实在不清楚了
private void timer1_Tick(object sender, EventArgs e)
{
string cconnect_string = @"Data Source=ip,1433;Network Library = DBMSSOCN;Initial Catalog=hycar_ll;user id=sa;pwd=1;Persist Security Info = true";
SqlConnection con = new SqlConnection(cconnect_string);
try
{
con.Open();
WriteLog("电信数据库链接成功!");
}
catch
{
WriteLog("电信数据库无法链接,请检查!");
}
string sql = "select * from mesge";
SqlDataAdapter da = new SqlDataAdapter(sql, con);
DataSet ds = new DataSet();
da.Fill(ds, "类型");
con.Close();
string date = DateTime.Now.Date.ToString("yyyy-MM-dd");
// MessageBox.Show(date);
string time = DateTime.Now.Hour.ToString() + "-" + DateTime.Now.Minute.ToString();
if (!Directory.Exists(@"d:\excel"))
{
Directory.CreateDirectory(@"d:\excel");
WriteLog("正在创建路径:" + @"d:\excel");
}
if (File.Exists(@"d:\excel\" + @"\" + date.Trim() + ".xls"))
{
File.Delete(@"d:\excel\" + date.Trim() + ".xls");
WriteLog(@"d:\excel\" + date.Trim() + ".xls");
}
string ss = @"d:\excel\" + date.Trim() + ".xls";
//doExport(ds, ss.Trim());
WriteLog(ss);
/////最主要是程序到了这一步不执行了导出excel
Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();
WriteLog("1");
if (excel == null)
{
WriteLog("你系统没有安装excel");
timer1.Start();
}
else
{
WriteLog("系统安装excel");
}
try
{
WriteLog("尝试写入excel");
int rowIndex = 1;
int colIndex = 0; excel.Application.Workbooks.Add(true);
Microsoft.Office.Interop.Excel.Workbook workbook = excel.Workbooks.Add(Microsoft.Office.Interop.Excel.XlWBATemplate.xlWBATWorksheet);
Microsoft.Office.Interop.Excel.Worksheet worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Worksheets[1];
Microsoft.Office.Interop.Excel.Range range;
excel.Visible = false;
//range = worksheet.get_Range(excel.Cells[2, 1], excel.Cells[rowNumber + 1, columnNumber]);
System.Data.DataTable table = ds.Tables[0];
foreach (DataColumn col in table.Columns)
{
colIndex++;
excel.Cells[1, colIndex] = col.ColumnName;
}
foreach (DataRow row in table.Rows)
{
rowIndex++;
colIndex = 0;
foreach (DataColumn col in table.Columns)
{
colIndex++;
range = worksheet.get_Range(excel.Cells[2, 1], excel.Cells[rowIndex, colIndex]);
range.NumberFormat = "@";
excel.Cells[rowIndex, colIndex] = row[col.ColumnName].ToString();
}
}
excel.Visible = false;
//excel.Sheets[0] = "消息";
excel.DisplayAlerts = false;
excel.ActiveWorkbook.SaveCopyAs(ss); excel.Quit();
excel = null;
//MessageBox.Show("数据导出成功!", "消息", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
}
catch (Exception ex)
{
WriteLog("错误信息: "+ex.Message);
// MessageBox.Show(ex.ToString());
}
finally
{
WriteLog("结束");
GC.Collect();//垃圾回收
timer1.Start();
}
}
下面再贴一下我的日志文件内容:
开始链接数据库!
电信数据库链接成功!
正在创建路径:d:\excel
d:\excel\2012-03-27.xls
电信数据库链接成功!
d:\excel\2012-03-27.xls
电信数据库链接成功!
d:\excel\2012-03-27.xls
电信数据库链接成功!
d:\excel\2012-03-27.xls
电信数据库链接成功!
一直在执行这一步,后面为什么不执行了呢 日志也不写 ,跪求各位大神帮忙
分不多但却是是拿不出来了
private void timer1_Tick(object sender, EventArgs e)
{
string cconnect_string = @"Data Source=ip,1433;Network Library = DBMSSOCN;Initial Catalog=hycar_ll;user id=sa;pwd=1;Persist Security Info = true";
SqlConnection con = new SqlConnection(cconnect_string);
try
{
con.Open();
WriteLog("电信数据库链接成功!");
}
catch
{
WriteLog("电信数据库无法链接,请检查!");
}
string sql = "select * from mesge";
SqlDataAdapter da = new SqlDataAdapter(sql, con);
DataSet ds = new DataSet();
da.Fill(ds, "类型");
con.Close();
string date = DateTime.Now.Date.ToString("yyyy-MM-dd");
// MessageBox.Show(date);
string time = DateTime.Now.Hour.ToString() + "-" + DateTime.Now.Minute.ToString();
if (!Directory.Exists(@"d:\excel"))
{
Directory.CreateDirectory(@"d:\excel");
WriteLog("正在创建路径:" + @"d:\excel");
}
if (File.Exists(@"d:\excel\" + @"\" + date.Trim() + ".xls"))
{
File.Delete(@"d:\excel\" + date.Trim() + ".xls");
WriteLog(@"d:\excel\" + date.Trim() + ".xls");
}
string ss = @"d:\excel\" + date.Trim() + ".xls";
//doExport(ds, ss.Trim());
WriteLog(ss);
/////最主要是程序到了这一步不执行了导出excel
Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();
WriteLog("1");
if (excel == null)
{
WriteLog("你系统没有安装excel");
timer1.Start();
}
else
{
WriteLog("系统安装excel");
}
try
{
WriteLog("尝试写入excel");
int rowIndex = 1;
int colIndex = 0; excel.Application.Workbooks.Add(true);
Microsoft.Office.Interop.Excel.Workbook workbook = excel.Workbooks.Add(Microsoft.Office.Interop.Excel.XlWBATemplate.xlWBATWorksheet);
Microsoft.Office.Interop.Excel.Worksheet worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Worksheets[1];
Microsoft.Office.Interop.Excel.Range range;
excel.Visible = false;
//range = worksheet.get_Range(excel.Cells[2, 1], excel.Cells[rowNumber + 1, columnNumber]);
System.Data.DataTable table = ds.Tables[0];
foreach (DataColumn col in table.Columns)
{
colIndex++;
excel.Cells[1, colIndex] = col.ColumnName;
}
foreach (DataRow row in table.Rows)
{
rowIndex++;
colIndex = 0;
foreach (DataColumn col in table.Columns)
{
colIndex++;
range = worksheet.get_Range(excel.Cells[2, 1], excel.Cells[rowIndex, colIndex]);
range.NumberFormat = "@";
excel.Cells[rowIndex, colIndex] = row[col.ColumnName].ToString();
}
}
excel.Visible = false;
//excel.Sheets[0] = "消息";
excel.DisplayAlerts = false;
excel.ActiveWorkbook.SaveCopyAs(ss); excel.Quit();
excel = null;
//MessageBox.Show("数据导出成功!", "消息", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
}
catch (Exception ex)
{
WriteLog("错误信息: "+ex.Message);
// MessageBox.Show(ex.ToString());
}
finally
{
WriteLog("结束");
GC.Collect();//垃圾回收
timer1.Start();
}
}
下面再贴一下我的日志文件内容:
开始链接数据库!
电信数据库链接成功!
正在创建路径:d:\excel
d:\excel\2012-03-27.xls
电信数据库链接成功!
d:\excel\2012-03-27.xls
电信数据库链接成功!
d:\excel\2012-03-27.xls
电信数据库链接成功!
d:\excel\2012-03-27.xls
电信数据库链接成功!
一直在执行这一步,后面为什么不执行了呢 日志也不写 ,跪求各位大神帮忙
分不多但却是是拿不出来了
解决方案 »
- 怎样用服务器代码给HTML控件赋值??(急!!!!)
- 关于类似用PDP屏显示滚动信息的软件
- 请教高手服务器端的WebService加了用户和密码验证,客户端要连接他怎么设置才能访问其中的方法?
- 数据绑定出现的问题!!在线等!!!!!!!!
- 求解释Robert算子算法,高手来啊!
- 关于绑定后数据刷新的问题。
- 在VS中提示未引入命名空间的类的问题
- c#控件随窗体自动缩放时,出现由最小化变为最大化时,界面出现混乱(附代码,敬请指点)
- 帮朋友求在新加坡的工作的软件工程师 c# vc.c++.
- 将字符串转换成时间格式
- gridview分页导出的问题
- C# 如何截取一段内容,不要样式表,标点符号也要获取的
WriteLog(ss);
/////最主要是程序到了这一步不执行下面导出excel的操作了
Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();
WriteLog("1");
急求大神,小神,小鬼,大鬼,各路神仙 显现能吧 ,帮帮小弟下
你给点意见 你觉得这个引用是错误的吗?
区域语言设置 怎么弄呢