读书人

关于 windows服务 操作excel有关问题

发布时间: 2012-04-04 16:38:51 作者: rapoo

关于 windows服务 操作excel问题
下面是我的代码:请教高手指点下 我错误的地方实在不清楚了
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
电信数据库链接成功!
一直在执行这一步,后面为什么不执行了呢 日志也不写 ,跪求各位大神帮忙
分不多但却是是拿不出来了

[解决办法]
microsoft office 12.0 object library是2007的,应该是可以兼容excel 2003,比如你的excel是中文版的,但是电脑的region里面设置的区域不是中国,也会出现问题,具体要try catch一下才知道,看看到底是什么原因造成的

读书人网 >C#

热点推荐