关于 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 (DataColum