数据导入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);
System.Runtime.InteropServices.Marshal.ReleaseComObject(currentWorkSheet);
System.Runtime.InteropServices.Marshal.ReleaseComObject(mySheets);
System.Runtime.InteropServices.Marshal.ReleaseComObject(currentWorkBook);
System.Runtime.InteropServices.Marshal.ReleaseComObject(myWorkBooks);
System.Runtime.InteropServices.Marshal.ReleaseComObject(excelApplication);
currentRange = null;
currentWorkSheet = null;
mySheets = null;
currentWorkBook = null;
myWorkBooks = null;
excelApplication = null;
GC.Collect();
}
private void SetSheetName(Worksheet sheet, string sheetName)
{
sheet.Name = sheetName;
}
public void SetValue(string position1,
string position2,
string value,
bool isBold,
bool isCenter)
{
try
{
if (position1.Equals(position2))
currentRange = currentWorkSheet.get_Range(position1, Missing.Value);
else
currentRange = currentWorkSheet.get_Range(position1, position2);
currentRange.Value2 = value;
/*
if (currentRange.NumberFormat.ToString().
IndexOfAny(new char[] { 'y ', 'm ', }) == -1)
currentRange.NumberFormat = "@ ";
*/
if (isBold)
{
currentRange.Font.Name = "宋体 ";
currentRange.Font.Size = 13;
currentRange.Font.Bold = true;
}
if (isCenter)
{
currentRange.HorizontalAlignment = XlHAlign.xlHAlignCenter;
currentRange.VerticalAlignment = XlVAlign.xlVAlignCenter;
}
}
catch (Exception ex)
{
CloseExcel();
throw new Exception( "in SetValue: " + ex.Message);
}
}
}
用法:
WriteExcel excel = new WriteExcel(1);
excel.CreateExcel( fileName, sheetName);
excel.SetValue(2, 2, filedNames[loop1], true, true);
excel.SaveExcel(true);
[解决办法]
Excel.Application myExcel = new Excel.ApplicationClass();
myExcel.Workbooks.Add(true);
Excel.Workbooks workbooks = myExcel.Workbooks;
Excel.Workbook workbook = workbooks.Add(Excel.XlWBATemplate.xlWBATWorksheet);
myExcel.Cells[1, 1] = label.text.ToString();
[解决办法]
楼上的 你不需要的好想太多了吧