首先添加Microsoft.office.interop.excel的引用
在引用过程中可能会出现Microsoft.Office 11.0 Object Library引用不上,这个可能是你的Office2003没有选择支持.net导致的,你可以再次运行office的安装文件,然后找到excel项展开节点,选择支持.net
接着在页面里using Namespace:
using Excel = Microsoft.Office.Interop.Excel;
以下是详细代码:
#region Export Data
/// <summary>
///
/// </summary>
private void Export()
{
Excel.Application excel = new Excel.Application();
Excel.Workbooks workbooks = excel.Workbooks;
Excel.Workbook workbook = workbooks.Add(Excel.XlWBATemplate.xlWBATWorksheet);
Excel.Worksheet worksheet = (Excel.Worksheet)workbook.Worksheets[1];
Excel.Range range = null;
worksheet.Cells[1, 1] = "报关单Excel";
range = (Excel.Range)worksheet.Cells[1, 1];
range.Font.Bold = true;
range.Font.Size = 18;
//range.Interior.Color = 150; //根据索引设置颜色,索引及值对照参考resource/HelpPic
range.HorizontalAlignment = Excel.XlHAlign.xlHAlignCenter;//设置文本居中
range = (Excel.Range)worksheet.get_Range(worksheet.Cells[1, 1], worksheet.Cells[1, 5]);
range.Select();
range.Merge(false);
range.Borders[Excel.XlBordersIndex.xlInsideHorizontal].Weight = Excel.XlBorderWeight.xlThin;
string FileName = Server.MapPath("..//..//resource//Templetes//") + DateTime.Today.ToString("yyyyMMdd") + new Random(DateTime.Now.Millisecond).Next(1000000).ToString() + ".xls";
//Foreach IList,And Add Data To Excel
//Write Excel Header
worksheet.Cells[3, 1] = "报关单号";
worksheet.Cells[3, 2] = "运输工具";
worksheet.Cells[3, 3] = "提运单号";
worksheet.Cells[3, 4] = "核销单号";
worksheet.Cells[3, 5] = "申报单位";
worksheet.Cells[3, 6] = "经营单位";
worksheet.Cells[3, 7] = "收/发货单位";
worksheet.Cells[3, 8] = "申报日期";
worksheet.Cells[3, 9] = "处理状态";
worksheet.Cells[3, 10] = "进口/出口";
worksheet.Cells[3, 11] = "关区";
worksheet.Cells[3, 12] = "备注";
worksheet.Cells[3, 13] = "更新时间";
//Write Excexl Body
int flag = 3;
foreach (var w in lst)
{
flag += 1;
worksheet.Cells[flag, 1] = w.entry_id_new ;
worksheet.Cells[flag, 2] = w.TRAF_NAME;
worksheet.Cells[flag, 3] = w.BILL_NO;
worksheet.Cells[flag, 4] = w.APPR_NO;
worksheet.Cells[flag, 5] = w.DECL_NAME;
worksheet.Cells[flag, 6] = w.TRADE_NAME;
worksheet.Cells[flag, 7] = w.SHIPPER;
worksheet.Cells[flag, 8] = w.DECL_DATE;
worksheet.Cells[flag, 9] = w.PROCESS_STATUS;
worksheet.Cells[flag, 10] = w.EXP_IMP;
worksheet.Cells[flag, 11] = w.SUB_CUS;
worksheet.Cells[flag, 12] = w.NOTES;
worksheet.Cells[flag, 13] = w.UPDATE_TIME;
}
try
{
workbook.Saved = true;
workbook.SaveCopyAs(FileName);
}
catch (Exception ex)
{
throw (ex);
}
workbooks.Close();
if (excel != null)
{
excel.Workbooks.Close();
excel.Quit();
int Generation = System.GC.GetGeneration(excel);
System.Runtime.InteropServices.Marshal.ReleaseComObject(excel);
excel = null;
GC.Collect(Generation);
}
GC.Collect();//强行销毁
//关闭最近打开的Excel进程
System.Diagnostics.Process[] excelProc = System.Diagnostics.Process.GetProcessesByName("EXCEL");
System.DateTime startTime = new DateTime();
int m, killId = 0;
for (m = 0; m < excelProc.Length; m++)
{
if (startTime < excelProc
.StartTime)
{
startTime = excelProc.StartTime;
killId = m;
}
}
if (excelProc[killId].HasExited == false)
{
excelProc[killId].Kill();
}
//提供下载
System.IO.FileInfo file = new System.IO.FileInfo(FileName);
Response.Clear();
Response.Charset = "UTF-8";
Response.ContentEncoding = System.Text.Encoding.UTF8;
// 添加头信息,为"文件下载/另存为"对话框指定默认文件名
Response.AddHeader("Content-Disposition", "attachment; filename=" + Server.UrlEncode(file.Name));
// 添加头信息,指定文件大小,让浏览器能够显示下载进度
Response.AddHeader("Content-Length", file.Length.ToString());
// 指定返回的是一个不能被客户端读取的流,必须被下载
Response.ContentType = "Application/ms-excel";
// 把文件流发送到客户端
Response.WriteFile(file.FullName);
}
#endregion
刚帮朋友写了个导入,命名没有规范,也没有封装什么。凑合着看吧。懒得改了
protected void Button1_Click(object sender, EventArgs e)
{
string strConn = "server=.;database=dydhb;uid=sa;pwd=111111";
SqlConnection sqlConn = new SqlConnection(strConn);
sqlConn.Open();
string FilePath = Server.MapPath("Book.xls");
object objOpt = System.Reflection.Missing.Value;
string a, b, c, d, e1;
string str = "insert into address_book(bumen,zhiwei,name,office_tel,phone,password) values(@bumen,@zhiwei,@name,@office_tel,@phone,@password)";
Excel.Application excel = new Excel.Application();
Excel.Workbooks workbooks = excel.Workbooks;
Excel.Workbook workbook = workbooks.Open(FilePath, objOpt, objOpt, objOpt, objOpt, objOpt, objOpt, objOpt, objOpt, objOpt, objOpt, objOpt, objOpt, objOpt, objOpt);
Excel.Worksheet worksheet = (Excel.Worksheet)workbook.Worksheets[1];
for (int i = 2; i <= worksheet.UsedRange.Rows.Count; i++)
{
a = ((Excel.Range)(worksheet.Cells[i, 2])).Text.ToString();
b = ((Excel.Range)(worksheet.Cells[i, 3])).Text.ToString();
c = ((Excel.Range)(worksheet.Cells[i, 4])).Text.ToString();
d = ((Excel.Range)(worksheet.Cells[i, 5])).Text.ToString();
e1 =((Excel.Range)(worksheet.Cells[i, 6])).Text.ToString();
SqlCommand sqlCmd = new SqlCommand(str, sqlConn);
sqlCmd.Parameters.AddWithValue("@bumen", a);
sqlCmd.Parameters.AddWithValue("@zhiwei", b);
sqlCmd.Parameters.AddWithValue("@name", c);
sqlCmd.Parameters.AddWithValue("@office_tel", d);
sqlCmd.Parameters.AddWithValue("@phone", e1);
sqlCmd.Parameters.AddWithValue("@password", System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(e1, "md5"));
sqlCmd.ExecuteNonQuery();
}
sqlConn.Close();
workbooks.Close();
if (excel != null)
{
excel.Workbooks.Close();
excel.Quit();
int Generation = System.GC.GetGeneration(excel);
System.Runtime.InteropServices.Marshal.ReleaseComObject(excel);
excel = null;
GC.Collect(Generation);
}
GC.Collect();//强行销毁
//关闭最近打开的Excel进程
System.Diagnostics.Process[] excelProc = System.Diagnostics.Process.GetProcessesByName("EXCEL");
System.DateTime startTime = new DateTime();
int m, killId = 0;
for (m = 0; m < excelProc.Length; m++)
{
if (startTime < excelProc
.StartTime)
{
startTime = excelProc
.StartTime;
killId = m;
}
}
if (excelProc[killId].HasExited == false)
{
excelProc[killId].Kill();
}
}