在网上看了一些例子,基本都是用类似下面的语句开始的。
Excel.Application excelApp= new Excel.ApplicationClass();请问需要引入什么程序集和命名空间呢。(using ......)我用的是VS2005。希望大家给我个完整的例子最好啊。谢谢大家!
Excel.Application excelApp= new Excel.ApplicationClass();请问需要引入什么程序集和命名空间呢。(using ......)我用的是VS2005。希望大家给我个完整的例子最好啊。谢谢大家!
解决方案 »
- C# out关键字 什么情况下使用比较好
- 为何(?<aid>.*?),(?<mp>.*?),(?<time>.*?),(?<msg>.*?)找不到msg?
- 给分100问一个实用的问题?
- 请问以下语句改为C#中的写法?
- 我刚学C#请教一下高手们;关于THIS的
- [求助]如何通过编程解决"access操作必须使用一个可更新的查询"问题?
- ~~~~~~~~~~~~~高手来帮看看吧!我实在是没办法了~~~~~~~~~
- C# 如何读取 SQL 的某一列的 描述 里的内容 急急急!!!!!?
- .net的msdn不可以用了,只出现个空架子,里面什么内容也没有,这个问题该如何解决?
- 要出售一个软件给某公司,要求限制该软件在那个公司的安装数量,请问该如何实现?
- 怎么在项目中使用面向对象的知识
- c# 的generics究竟有什么用啊?那位大虾给个深入浅出的例子讲解一下?谢谢
引入Excel11.0
引用---添加引用----COM-----Microsoft Excel 11.0 Object Library
public static void ExportToExcel(string[,] exportData, string exportFile, string templateFile, string templetFilePath, string downtemplatePath)
{
//解决excel bug
System.Globalization.CultureInfo CurrentCI = System.Threading.Thread.CurrentThread.CurrentCulture;
System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-US");
////////////////////////////////////////////////////////////////////////////////////////////////////////
Excel._Application app;
Excel.Workbook workBook;
Excel.Worksheet workSheet;
Excel.Range range; app = new Excel.ApplicationClass();
app.Visible = false;
workBook = app.Workbooks.Open(templetFilePath + templateFile, Missing.Value, Missing.Value, Missing.Value,
Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value,
Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);
workSheet = (Excel.Worksheet)workBook.Sheets.get_Item(2); // Read parameters from the second worksheet of xls. int datasheet = Convert.ToInt32((workSheet.get_Range("B1", Missing.Value)).Value2);
int startRow = Convert.ToInt32((workSheet.get_Range("B2", Missing.Value)).Value2);
int startCol = Convert.ToInt32((workSheet.get_Range("B3", Missing.Value)).Value2); workSheet = (Excel.Worksheet)workBook.Sheets.get_Item(datasheet);
int rowCount = exportData.GetLength(0);
int colCount = exportData.GetLength(1);
range = (Excel.Range)workSheet.Cells[startRow, startCol]; //写入Excel的坐标
range = range.get_Resize(rowCount, colCount);
range.Value2 = exportData; workBook.SaveAs(downtemplatePath + exportFile, Missing.Value, Missing.Value, Missing.Value, Missing.Value,
Missing.Value, Excel.XlSaveAsAccessMode.xlExclusive, Missing.Value,
Missing.Value, Missing.Value, Missing.Value, Missing.Value); workBook.Close(null, null, null);
app.Workbooks.Close();
app.Quit(); if (range != null)
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(range);
range = null;
} if (workSheet != null)
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(workSheet);
workSheet = null;
}
if (workBook != null)
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(workBook);
workBook = null;
}
if (app != null)
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(app);
app = null;
} GC.Collect();
////////////////////////////////////////////////////////////////////////////////////////////////////////
//恢复语言环境
//System.Runtime.InteropServices.Marshal.ReleaseComObject(xlRang);
System.Threading.Thread.CurrentThread.CurrentCulture = CurrentCI;
////////////////////////////////////////////////////////////////////////////////////////////////////////
DownLoadFile(downtemplatePath, exportFile);
DeleteFile(downtemplatePath + exportFile); }Interop.Excel.dll
http://blog.csdn.net/cpp2017/archive/2008/04/02/2245276.aspx
http://blog.csdn.net/cpp2017/archive/2008/04/02/2245396.aspxhttp://www.icgbbs.com
public static bool ExportForDataGridview(DataGridView gridView, string fileName, bool isShowExcle)
{ //建立Excel对象 Microsoft.Office.Interop.Excel.Application app = new Microsoft.Office.Interop.Excel.Application();
try
{
if (app == null)
{
return false;
}
app.Visible = isShowExcle;
Workbooks workbooks = app.Workbooks;
_Workbook workbook = workbooks.Add(XlWBATemplate.xlWBATWorksheet);
Sheets sheets = workbook.Worksheets;
_Worksheet worksheet = (_Worksheet)sheets.get_Item(1);
if (worksheet == null)
{
return false;
}
string sLen = "";
//取得最后一列列名
char H = (char)(64 + gridView.ColumnCount / 26);
char L = (char)(64 + gridView.ColumnCount % 26);
if (gridView.ColumnCount < 26)
{
sLen = L.ToString();
}
else
{
sLen = H.ToString() + L.ToString();
}
//标题
string sTmp = sLen + "1";
Range ranCaption = worksheet.get_Range(sTmp, "A1");
string[] asCaption = new string[gridView.ColumnCount];
for (int i = 0; i < gridView.ColumnCount; i++)
{
asCaption[i] = gridView.Columns[i].HeaderText;
}
ranCaption.Value2 = asCaption; //数据
object[] obj = new object[gridView.Columns.Count];
for (int r = 0; r < gridView.RowCount - 1; r++)
{
for (int l = 0; l < gridView.Columns.Count; l++)
{
if (gridView[l, r].ValueType == typeof(DateTime))
{
obj[l] = gridView[l, r].Value.ToString();
}
else
{
obj[l] = gridView[l, r].Value;
}
}
string cell1 = sLen + ((int)(r + 2)).ToString();
string cell2 = "A" + ((int)(r + 2)).ToString();
Range ran = worksheet.get_Range(cell1, cell2);
ran.Value2 = obj;
}
//保存
workbook.SaveCopyAs(fileName);
workbook.Saved = true;
}
finally
{
//关闭
app.UserControl = false;
app.Quit();
}
return true; }