ASP.NET对Excel单元格进行操作并且插入数据
protected void Button3_Click(object sender, EventArgs e)
{
try
{
DataSet ds = new BLL.TrafficLine().GetAllList();
System.Data.DataTable dt = ds.Tables[0];
DataView dv = dt.DefaultView;
////////////////////////////////////
Excel._Application app = new Excel.ApplicationClass();
app.Visible = false;
app.UserControl = true;
object missing = System.Reflection.Missing.Value;
Workbooks workbooks = app.Workbooks;
_Workbook workbook = workbooks.Add(XlWBATemplate.xlWBATWorksheet);
_Worksheet worksheet =(_Worksheet ) workbook.Worksheets.get_Item(1);
worksheet.get_Range(worksheet.Cells[3, 1], worksheet.Cells[3, 3]).Merge(missing);
worksheet.get_Range(worksheet.Cells[3, 1], worksheet.Cells[3, 1]).Value2 = DateTime.Now.ToLocalTime().ToString();
worksheet.get_Range(worksheet.Cells[3, 1], worksheet.Cells[3, 1]).Font.Bold = true;
worksheet.get_Range(worksheet.Cells[3, 1], worksheet.Cells[3, 1]).Borders.Weight = 4; //设置单元格的逼边框厚度,为了打印能显示出线条
worksheet.Cells[3, 4] = ds.Tables[0].Rows[1]["LineName"].ToString();
string str = DateTime.Now.Millisecond.ToString();
app.Save(str); //str为一个唯一值
}
catch { }
finally
{
foreach (System.Diagnostics.Process pro in System.Diagnostics.Process.GetProcessesByName("EXCEL"))
{
pro.Kill();
}
}
}