First add a reference to Microsoft.office.interop.excel
During the reference process, Microsoft.Office 11.0 Object Library may not be referenced. This may be caused by your Office2003 not choosing to support .net. You can run the office installation file again, and then find the excel item to expand the node and select support. net
Then use Namespace in the page:
using Excel = Microsoft.Office.Interop.Excel;
The following is the detailed code:
#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; //Set the color according to the index, index and value reference resource/HelpPic
range.HorizontalAlignment = Excel.XlHAlign.xlHAlignCenter;//Set the text to center
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] = "Customs Declaration Number";
worksheet.Cells[3, 2] = "Means of Transport";
worksheet.Cells[3, 3] = "Bill of Lading Number";
worksheet.Cells[3, 4] = "Core Sales order number";
worksheet.Cells[3, 5] = "declaration unit";
worksheet.Cells[3, 6] = "business unit";
worksheet.Cells[3, 7] = "Receiving/shipping unit";
worksheet.Cells[3, 8] = "declaration date";
worksheet.Cells[3, 9] = "processing status";
worksheet.Cells[3, 10] = "import/export";
worksheet.Cells[3, 11 ] = "Close Area";
worksheet.Cells[3, 12] = "Remarks";
worksheet.Cells[3, 13] = "Update Time";
//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();//强行销毁
//Close the recently opened Excel process
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(); } //Provide download System.IO.FileInfo file = new System.IO.FileInfo(FileName); Response.Clear(); Response.Charset = "UTF-8"; Response.ContentEncoding = System.Text.Encoding.UTF8; // Add header information for "file download/save as Specify a default filename for the dialog
Response.AddHeader("Content-Disposition", "attachment; filename=" + Server.UrlEncode(file.Name));
// Add header information, specify the file size, so that the browser can display the download progress
Response.AddHeader("Content -Length", file.Length.ToString());
// Specifies that the returned stream is a stream that cannot be read by the client and must be downloaded
Response.ContentType = "Application/ms-excel";
// Send the file stream To client
Response.WriteFile(file.FullName);
}
#endregion
I just wrote an import for a friend, the naming is not standardized, and there is no package. Just make do with it. too lazy to change
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();//强行销毁
//Close the recently opened Excel process
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();
}
}