日期:2014-05-18  浏览次数:20478 次

数据导入excel的问题
请问在.net中如何实现将一个lable中的数据导入到excel的Sheet1指定的一个单元格内?

------解决方案--------------------
up
------解决方案--------------------
你可以看下 "孟子E章 "的网站,应该有

------解决方案--------------------
刚刚把 导入 导出 做完
但是没有做你说的功能!!

呵呵
------解决方案--------------------
以前自己写的一个类,摘出一部分,你自己参考吧
public class WriteExcel
{
private Application excelApplication;
private Workbooks myWorkBooks;
private Workbook currentWorkBook;
private Sheets mySheets;
private Worksheet currentWorkSheet;
private Range currentRange = null;
private string fileName;

public WriteExcel(int sheetCount)
{
//
// TODO: 在此处添加构造函数逻辑
//
excelApplication = new Application();
excelApplication.Visible = false;
excelApplication.SheetsInNewWorkbook = sheetCount;
myWorkBooks = excelApplication.Workbooks;

}
public void CreateExcel(string fileName,string sheetName)
{
this.fileName = fileName;
try
{
currentWorkBook = myWorkBooks.Add(XlWBATemplate.xlWBATWorksheet);
mySheets = currentWorkBook.Sheets;
currentWorkSheet = currentWorkBook.ActiveSheet as Worksheet;
SetSheetName(currentWorkSheet, sheetName);
}
catch (Exception ex)
{
CloseExcel();
throw new Exception( "in CreateExcel: " + ex.Message);
}

}
public void SaveExcel(bool isFirst)
{
try
{
currentWorkSheet.Columns.AutoFit();
if (isFirst)
{
string fullFileName = fileName.Substring(0, fileName.LastIndexOf( '\\ ') + 1)
+ DateTime.Now.ToString( "yyyy-MM-dd-HH-mm-fffffff ")
+ fileName.Substring(fileName.LastIndexOf( '\\ ') + 1);
fileName = fullFileName;
currentWorkBook.SaveAs(fullFileName, Missing.Value, Missing.Value,
Missing.Value, Missing.Value, Missing.Value, XlSaveAsAccessMode.xlNoChange,
Missing.Value, Missing.Value, Missing.Value, Missing.Value,
Missing.Value);
}
else
{
currentWorkBook.Save();
}
//关闭
CloseExcel();
}
catch (Exception ex)
{
CloseExcel();
}
}
/*
* 关闭excel文件
*/
public void CloseExcel()
{
if (currentWorkBook != null)
currentWorkBook.Close(Missing.Value, Missing.Value, Missing.Value);
if (myWorkBooks != null && myWorkBooks.Count == 0)
myWorkBooks.Close();
if (excelApplication != null)
excelApplication.Quit();

if (currentRange != null)
System.Runtime.InteropServices.Marshal.ReleaseComObject(currentRange);