本文整理汇总了C#中Excel.Application类的典型用法代码示例。如果您正苦于以下问题:C# Excel.Application类的具体用法?C# Excel.Application怎么用?C# Excel.Application使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
Excel.Application类属于命名空间,在下文中一共展示了Excel.Application类的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的C#代码示例。
示例1: GetExcelSheetName
/// <summary>
/// 读取Excel文件
/// </summary>
/// <param name="pPath"></param>
/// <returns></returns>
private string GetExcelSheetName(string pPath)
{
//打开一个Excel应用
_excelApp = new Excel.Application();
if (_excelApp == null)
{
throw new Exception("打开Excel应用时发生错误!");
}
_books = _excelApp.Workbooks;
//打开一个现有的工作薄
_book = _books.Add(pPath);
_sheets = _book.Sheets;
//选择第一个Sheet页
_sheet = (Excel._Worksheet)_sheets.get_Item(1);
string sheetName = _sheet.Name;
ReleaseCOM(_sheet);
ReleaseCOM(_sheets);
ReleaseCOM(_book);
ReleaseCOM(_books);
_excelApp.Quit();
ReleaseCOM(_excelApp);
return sheetName;
}
开发者ID:uwitec,项目名称:gloryview-rfid,代码行数:31,代码来源:ExcelHelper.cs
示例2: CreateStudentBaseSimpleTable
public void CreateStudentBaseSimpleTable()
{
KillProcess();
try
{
m_objExcel = new Excel.Application();
m_objBooks = (Excel.Workbooks)m_objExcel.Workbooks;
m_objBook = (Excel._Workbook)(m_objBooks.Open( //打开该文件
[email protected]"\report\ImportBaseTableForStudent(simple).xls",Type.Missing,Type.Missing,Type.Missing,
Type.Missing,Type.Missing,Type.Missing,Type.Missing,
Type.Missing,Type.Missing,Type.Missing,Type.Missing,
Type.Missing));
m_objExcel.Visible = true;
}
catch(Exception e)
{
Util.WriteLog(e.Message,Util.EXCEPTION_LOG_TITLE);
}
finally
{
m_objSheet = null;
m_objSheets = null;
m_objBook = null;
m_objBooks = null;
m_objExcel = null;
GC.Collect();
}
}
开发者ID:zesus19,项目名称:c4.v2.T,代码行数:32,代码来源:OptionRules.cs
示例3: StartupApp
// 启动Excel 程序
public bool StartupApp(bool visible)
{
// 检查是否有Excel正在运行
bool flag = false;
foreach (var item in Process.GetProcesses())
{
if (item.ProcessName == "EXCEL")
{
flag = true;
break;
}
}
if (!flag)
{
ExcelApp = new Excel.Application();
}
else
{
object obj = Marshal.GetActiveObject("Excel.Application"); // 引用已在执行的Excel
ExcelApp = obj as Excel.Application;
}
ExcelApp.Visible = visible; // Excel程序不可见
return true;
}
开发者ID:WangXiang10,项目名称:xinli,代码行数:27,代码来源:ExcelStream.cs
示例4: Button1_Click
private void Button1_Click(object sender, EventArgs e)
{
Excel.Application xl = new Excel.Application();
xl.Visible = true;
xl.Workbooks.Add("c:\\Pasta1.xlt");
xl.Cells[3, 4] = TextBox1.Text;
}
开发者ID:tca85,项目名称:ASP.NET,代码行数:10,代码来源:Form1.cs
示例5: ExcelController
/// <summary>
/// エクセルデータの操作を行う
/// </summary>
/// <param name="path">xlsファイルパス</param>
public ExcelController(string path)
{
exPath = path;
oXls = new Excel.Application();
//Excel画面を表示しない
oXls.Visible = false;
//Excelファイルをオープンする
oWBook = (Excel.Workbook)(oXls.Workbooks.Open(exPath));
}
开发者ID:nallpo,项目名称:CSharpUtility,代码行数:16,代码来源:ExcelController.cs
示例6: ExportExcel
public void ExportExcel()
{
try
{
//if (e.KeyCode != Keys.F12) return;
if (_autoExcel == false) return;
BindingSource bsTemp = this.PrimaryGrid.DataSource as BindingSource;
if (bsTemp == null) return;
DataTable tempDATA = bsTemp.DataSource as DataTable;
if (tempDATA == null) return;
DataTable DATA = tempDATA;
DATA.RejectChanges();
Excel.Application objExcel;
try
{
//Tìm instance Excel đang chạy.
objExcel = (Excel.Application)System.Runtime.InteropServices.Marshal.GetActiveObject("Excel.Application");
}
catch
{
//Không có instance nào của Excel đang chạy.
objExcel = new Excel.Application();
}
try
{
objExcel.Interactive = false;
objExcel.Interactive = true;
}
catch
{
//CrossShow2 msg = new CrossShow2(clsMx.Show);
//this.Invoke(msg, Qk.Settings.ctrmsgPrintError, eAlertType.CanhBao);
return;
}
objExcel.Visible = false;
string tmpFileXSL = Path.Combine(st.TEMP_DIR, Guid.NewGuid().ToString() + ".xls");
clsAll.ExtractXLS("AutoExcel.xls", tmpFileXSL);
objExcel.Workbooks.Open(tmpFileXSL);
object[,] objData = clsAll.DataTable2ArrayObjects(DATA);
string strRange = string.Format("A{0}:{1}{2}", 1, clsAll.GetExcelColumnLabel(DATA.Columns.Count), DATA.Rows.Count);
objExcel.Range[strRange].Value = objData;
objExcel.Visible = true;
objExcel.ActiveWorkbook.Save();
//objExcel.Worksheets.PrintPreview();
}
catch (Exception ex)
{
clsMx.Show(ex, this.Name);
}
}
开发者ID:huamanhtuyen,项目名称:VNACCS,代码行数:53,代码来源:tGrid.cs
示例7: LoadDataExcel
private void LoadDataExcel()
{
try
{
if (tbUploadFile.Text == "")
{
MessageBox.Show("Vui lòng load file để cập nhật!");
}
string status_name = "", status_code = "", result = "";
string resultT = "" , MSG = "";
conTTTSOA SOA = new conTTTSOA();
status_name = cbTrangthai.Text.ToString();
status_code = cbTrangthai.SelectedValue.ToString();
// string file = @"C:\Users\thongnt.NAMABANK0\Pictures\CẬP NHẬT TRẠNG THÁI ĐỒNG LOẠT FILE MẪU.xls";
Excel.Application xlApp = new Excel.Application();
Excel.Workbook xlWorkbook = xlApp.Workbooks.Open(tbUploadFile.Text);
//Excel.Workbook xlWorkbook = xlApp.Workbooks.Open(file);
Excel._Worksheet xlWorksheet = (Excel._Worksheet)xlWorkbook.Sheets[1];
Excel.Range xlRange = xlWorksheet.UsedRange;
string contract_number = "";
int n_true = 0;
for (int i = 1; i <= xlRange.Count; i++)
{
if ((string)(xlRange.Cells[i, 1] as Excel.Range).Value2.ToString() == "")
{
return;
}
contract_number = (string)(xlRange.Cells[i, 1] as Excel.Range).Value2.ToString();
result = SOA.SOA_Change_Contr_Status("Web", contract_number, status_code, status_name, "82");
resultT = result.Substring(result.IndexOf("<result>") + "<result>".Length, result.IndexOf("</result>") - (result.IndexOf("<result>") + "<result>".Length));
MSG = result.Substring(result.IndexOf("<msg>") + "<msg>".Length, result.IndexOf("</msg>") - (result.IndexOf("<msg>") + "<msg>".Length));
if(resultT == "0")
{
n_true = n_true + 1;
lbKetqua.Text = MSG + " " + n_true;
}
if (i == xlRange.Count)
{
MessageBox.Show("Đã hoàn thành cập nhật!!!!");
return;
}
}
}
catch (Exception error)
{
MessageBox.Show("File excel không đúng định dạng!",error.Message);
}
}
开发者ID:phongferrari,项目名称:Way4-CustomerCareForm,代码行数:52,代码来源:Frm_Update_Status_Multi.cs
示例8: NutritionPrint
public void NutritionPrint(string savePath)
{
try
{
m_objExcel = new Excel.Application();
m_objBooks = (Excel.Workbooks)m_objExcel.Workbooks;
m_objBook = (Excel._Workbook)m_objBooks.Open([email protected]"report\nutrition.xls",
m_objOpt,m_objOpt,m_objOpt,m_objOpt,m_objOpt,m_objOpt,m_objOpt,m_objOpt,m_objOpt,
m_objOpt,m_objOpt,m_objOpt);
m_objSheets = (Excel.Sheets)m_objBook.Worksheets;
int number = m_objSheets.Count;
writeCover();
writeStuAmount();
writeStuConvert();
writeACC1();
writeACC2();
writeElement();
m_objBook.SaveAs(savePath, m_objOpt, m_objOpt,
m_objOpt, m_objOpt, m_objOpt, Excel.XlSaveAsAccessMode.xlNoChange,
m_objOpt, m_objOpt, m_objOpt, m_objOpt);
m_objBook.Close(false, m_objOpt, m_objOpt);
m_objExcel.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(m_objRange);
System.Runtime.InteropServices.Marshal.ReleaseComObject(m_objSheet);
System.Runtime.InteropServices.Marshal.ReleaseComObject(m_objSheets);
System.Runtime.InteropServices.Marshal.ReleaseComObject(m_objBook);
System.Runtime.InteropServices.Marshal.ReleaseComObject(m_objBooks);
System.Runtime.InteropServices.Marshal.ReleaseComObject(m_objExcel);
}
catch(Exception ex)
{
Util.WriteLog(ex.Message,Util.EXCEPTION_LOG_TITLE);
}
finally
{
m_objRange = null;
m_objSheet = null;
m_objSheets = null;
m_objBook = null;
m_objBooks = null;
m_objExcel = null;
GC.Collect();
KillProcess();
}
}
开发者ID:zesus19,项目名称:c4.v2.T,代码行数:51,代码来源:FoodManagementPrintRules.cs
示例9: NativeExcel_CreateExcel
public static void NativeExcel_CreateExcel(
DataTable Dt
, ClsExcel_Columns Columns
, string SaveFileName = ""
, Excel.XlFileFormat FileFormat = Excel.XlFileFormat.xlExcel5
, string Title = "")
{
Excel.Application Obj_Excel = new Excel.Application();
Excel.Workbook owbook = Obj_Excel.Workbooks.Add();
Excel.Worksheet owsheet = owbook.Worksheets.Add();
Int32 RowCt = 2;
Int32 ColCt = 1;
foreach (ClsExcel_Columns.Str_Columns? Obj in Columns.pObj)
{
owsheet.Cells[RowCt, ColCt].Value = Obj.Value.FieldDesc;
owsheet.Cells[RowCt, ColCt].Font.Bold = true;
Excel.Range Inner_ExRange =
owsheet.Range[
Do_Methods.GenerateChr(ColCt)
+ RowCt.ToString()
+ ":"
+ Do_Methods.GenerateChr(ColCt)
+ (RowCt + Dt.Rows.Count).ToString()];
Inner_ExRange.NumberFormat = Obj.Value.NumberFormat;
ColCt++;
}
RowCt++;
ColCt = 1;
Excel.Range ExRange =
owsheet.Range[
Do_Methods.GenerateChr(ColCt)
+ RowCt.ToString()
+ ":"
+ Do_Methods.GenerateChr(ColCt + Columns.pObj.Count)
+ (RowCt + Dt.Rows.Count - 1).ToString()];
ExRange.Value = Do_Methods.ConvertDataTo2DimArray(Dt, Columns.pFieldName);
owsheet.Range["A1;IV65536"].AutoFit();
if (SaveFileName == "")
{ SaveFileName = "Excel_File"; }
owsheet.Range["A1:A1"].Select();
owbook.SaveAs(SaveFileName, FileFormat);
}
开发者ID:kolinlocke,项目名称:DamitApp,代码行数:50,代码来源:Layer01_Methods_Excel.cs
示例10: button1_Click
private void button1_Click(object sender, EventArgs e)
{
Excel.Application xl = new Excel.Application();
xl.Visible = true;
xl.Workbooks.Add("c:\\Pasta1.xlt");
xl.Cells[3, 4] = "oi";
object x = new object();
xl.Workbooks[0].Close(false, "", x);
xl.Quit();
}
开发者ID:tca85,项目名称:ASP.NET,代码行数:16,代码来源:Form1.cs
示例11: ExcelControl
public ExcelControl()
{
// This call is required by the Windows.Forms Form Designer.
InitializeComponent();
// TODO: Add any initialization after the InitComponent call
try
{
excelApp = new Excel.ApplicationClass();
excelApp.Visible = true;
Excel.Workbook book = excelApp.Workbooks.Open(@"C:\Documents and Settings\raghunandanr\Desktop\Benefit Design Modeler V3.28_ Blank.xls",Type.Missing,false,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing);
this.CreateGraphics().DrawString("ok ok",new System.Drawing.Font("Arial",25),Brushes.GreenYellow,10,10);
book.Close(false,Type.Missing,Type.Missing);
}
catch(Exception ex)
{
this.CreateGraphics().DrawString(ex.Message,new System.Drawing.Font("Arial",16),Brushes.Chocolate,10,10);
}
}
开发者ID:rags,项目名称:playground,代码行数:18,代码来源:ExcelControl.cs
示例12: Upload
public void Upload()
{
string strPathnew = Server.MapPath("~/") + "UploadFile\\AcrTestfinalresult.xls";
try
{
Excel.Workbook xlWorkBook;
Excel.Worksheet xlWorkSheet;
xlWorkBook = new Excel.Application().Workbooks.Add(Missing.Value);
xlWorkBook.Application.Visible = true;
xlWorkSheet = (Excel.Worksheet)xlWorkBook.ActiveSheet;
string strPath = Server.MapPath("~/") + "UploadFile\\AcrTestfinal.xls";
StreamReader sr = new StreamReader(strPath); //Read the Excel Stream
string strTest = "";
int i = 1;
while (!sr.EndOfStream)
{
strTest = sr.ReadLine();
string[] strData = strTest.Split('\t');
int count = strData.Length;
for (int k = 1; k <= count; k++)
{
string str = strData[k - 1].Replace("\"", "");
if (k == 4) //1 based index of Column required to be changed
{
str = str.Insert(0, "'");
}
xlWorkSheet.Cells[i, k] = str;
}
i++;
}
sr.Close();
sr.Dispose();
xlWorkSheet.Columns.AutoFit();
xlWorkBook.SaveAs(strPathnew , Excel.XlFileFormat.xlExcel4, Missing.Value, Missing.Value, false, false, Excel.XlSaveAsAccessMode.xlNoChange, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);
xlWorkBook.Close(Missing.Value, strPathnew, Missing.Value);
}
catch
{ }
}
开发者ID:krishnakant,项目名称:WMS,代码行数:44,代码来源:UpldTest.aspx.cs
示例13: ExportToExcel
public static void ExportToExcel(DataTable dt, string type, string title, string thoiGian)
{
//Export to excel
try
{
Excel.Application excel = new Excel.Application();
excel.Application.Workbooks.Add(true);
System.Data.DataTable table = dt;
excel.Cells[1, 1] = "Print Date :" + DateTime.Now.ToLongDateString();
excel.Cells[3, 1] = title;
excel.Cells[4, 1] = thoiGian;
for (int k = 0; k < table.Columns.Count; k++)
{
excel.Cells[6, k + 1] = table.Columns[k].ColumnName.ToString();
}
int ColumnIndex = 0;
int rowIndex = 5;
foreach (DataRow row in table.Rows)
{
rowIndex++;
ColumnIndex = 0;
foreach (DataColumn col in table.Columns)
{
ColumnIndex++;
((Excel.Range)excel.Cells[rowIndex + 1, ColumnIndex]).NumberFormat = "@"; //Format dạng text
//excel.Cells[rowIndex + 1, ColumnIndex] = row[col.ColumnName]; //OLD 2016-01-04 (3)
excel.Cells[rowIndex + 1, ColumnIndex] = row[col.ColumnName].ToString(); //2016-01-04 (3)
//((Excel.Range)excel.Cells[rowIndex + 1, ColumnIndex]).BorderAround(Excel.XlLineStyle.xlContinuous, Excel.XlBorderWeight.xlThin, Excel.XlColorIndex.xlColorIndexAutomatic, 1);
}
}
excel.Visible = true;
Excel._Worksheet worksheet = (Excel._Worksheet)excel.ActiveSheet;
worksheet.Activate();
}
catch (Exception exml)
{
MessageBox.Show(exml.Message);
}
}
开发者ID:phongferrari,项目名称:card-report,代码行数:44,代码来源:Frm_Main.cs
示例14: ToExcel
/// <summary>
/// Export DataList To Excel File
/// </summary>
/// <param name="dataList">List of Data need export</param>
/// <returns></returns>
public bool ToExcel(List<ArrayList> dataList)
{
Excel.Application xlApp = new Excel.Application();
Excel.Workbook xlWorkBook = xlApp.Workbooks.Open(_temp_excel_file_path);
Excel.Worksheet xlWorkSheet = xlWorkBook.Worksheets.get_Item(_sheet_number);
//SetValue and Fortmat base on template file
SetCells(xlWorkSheet, dataList);
//Save as new file
xlWorkSheet.SaveAs(_save_as_export_file_path);
//Dispose obj
xlWorkBook.Close();
xlApp.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(xlWorkSheet);
System.Runtime.InteropServices.Marshal.ReleaseComObject(xlWorkBook);
System.Runtime.InteropServices.Marshal.ReleaseComObject(xlApp);
xlWorkSheet = null;
xlWorkBook = null;
xlApp = null;
return true;
}
开发者ID:vuchannguyen,项目名称:lg-py,代码行数:28,代码来源:Export.cs
示例15: FinanceStatPrint
public void FinanceStatPrint(DataTable data, string className, DateTime date, string savePath)
{
DataTable dtPresents = new FinanInfoDataAccess().GetStudentPresents(date, className);
if (dtPresents == null || dtPresents.Rows.Count == 0)
{
throw new Exception("没有要使用的数据!");
}
else
{
data.Columns.AddRange(new DataColumn[]{ new DataColumn("小计", Type.GetType("System.Double")),
new DataColumn("stuPresent", Type.GetType("System.String")),
new DataColumn("stuAbsent", Type.GetType("System.String"))});
if (dtPresents.Rows.Count != data.Rows.Count)
{
throw new Exception("检测到数据完整性错误,请尝试重新生成数据!");
}
else
{
for (int i = 0; i < dtPresents.Rows.Count; i++)
{
data.Rows[i]["小计"] = 0;
data.Rows[i]["stuPresent"] = dtPresents.Rows[i]["times"];
data.Rows[i]["stuAbsent"] = dtPresents.Rows[i]["times_abs"];
}
}
m_objExcel = new Excel.Application();
m_objBooks = (Excel.Workbooks)m_objExcel.Workbooks;
m_objBook = (Excel._Workbook)m_objBooks.Open(excelPath + @"report\FinanceStat.xls",
m_objOpt,m_objOpt,m_objOpt,m_objOpt,m_objOpt,m_objOpt,m_objOpt,m_objOpt,m_objOpt,
m_objOpt,m_objOpt,m_objOpt);
m_objSheets = (Excel.Sheets)m_objBook.Worksheets;
m_objSheet = (Excel._Worksheet)m_objSheets.get_Item(1);
object[,] objData = new object[data.Rows.Count + 1, data.Columns.Count + 4];
for(int row = 0; row < data.Rows.Count; row++)
{
objData[row, 0] = row + 1;
for (int column = 0; column < data.Columns.Count - 3; column++)
{
if (column <= 2)
{
objData[row, column + 1] = data.Rows[row][column];
}
else if(column == 3)
{
objData[row, column + 1] = data.Rows[row][data.Columns.Count - 2];
objData[row, column + 2] = data.Rows[row][data.Columns.Count - 1];
double temp = Convert.ToDouble(data.Rows[row][column]);
objData[row, column + 3] = temp;
objData[data.Rows.Count, column + 3] = temp + (objData[data.Rows.Count, column + 3] == null ? 0 :
Convert.ToDouble(objData[data.Rows.Count, column + 3]));
}
else if (column >= 4 && column < 7)
{
double temp = Convert.ToDouble(data.Rows[row][column]);
objData[row, column + 3] = temp;
objData[data.Rows.Count, column + 3] = temp + (objData[data.Rows.Count, column + 3] == null ? 0 :
Convert.ToDouble(objData[data.Rows.Count, column + 3]));
}
else if (column == 7)
{
double temp1 = Convert.ToDouble(data.Rows[row][column - 4]) +
Convert.ToDouble(data.Rows[row][column - 3]) + Convert.ToDouble(data.Rows[row][column - 2]) +
Convert.ToDouble(data.Rows[row][column - 1]);
objData[row, column + 3] = temp1;
double temp2 = Convert.ToDouble(data.Rows[row][column]);
objData[row, column + 4] = temp2;
objData[data.Rows.Count, column + 3] = temp1 + (objData[data.Rows.Count, column + 3] == null ? 0 :
Convert.ToDouble(objData[data.Rows.Count, column + 3]));
objData[data.Rows.Count, column + 4] = temp2 + (objData[data.Rows.Count, column + 4] == null ? 0 :
Convert.ToDouble(objData[data.Rows.Count, column + 4]));
}
else
{
double temp = Convert.ToDouble(data.Rows[row][column]);
objData[row, column + 4] = temp;
objData[data.Rows.Count, column + 4] = temp + (objData[data.Rows.Count, column + 4] == null ? 0 :
Convert.ToDouble(objData[data.Rows.Count, column + 4]));
}
}
}
m_objRange = m_objSheet.get_Range("A6", m_objOpt);
m_objRange = m_objRange.get_Resize(data.Rows.Count + 1, data.Columns.Count + 1);
m_objRange.Value = objData;
m_objRange.VerticalAlignment = Excel.XlVAlign.xlVAlignCenter;
m_objRange.HorizontalAlignment = Excel.XlHAlign.xlHAlignCenter;
m_objRange.WrapText = true;
m_objRange.Borders.LineStyle = System.Windows.Forms.BorderStyle.FixedSingle;
m_objRange.Font.Size = 10;
m_objRange = m_objSheet.get_Range("G3", m_objOpt);
m_objRange.Value = "各项费用";
m_objRange = m_objSheet.get_Range(m_objSheet.Cells[3, 7], m_objSheet.Cells[4, data.Columns.Count]);
m_objRange.Merge(m_objOpt);
m_objRange.VerticalAlignment = Excel.XlVAlign.xlVAlignCenter;
m_objRange.HorizontalAlignment = Excel.XlHAlign.xlHAlignCenter;
m_objRange.WrapText = true;
//.........这里部分代码省略.........
开发者ID:zesus19,项目名称:c4.v2.T,代码行数:101,代码来源:FinanMgmtInfoPrintRules.cs
示例16: ExcelHelper
/// <summary>
/// 构造函数,将一个已有Excel工作簿作为模板,并指定输出路径
/// </summary>
/// <param name="templetFilePath">Excel模板文件路径</param>
/// <param name="outputFilePath">输出Excel文件路径</param>
public ExcelHelper(string templetFilePath, string outputFilePath)
{
if (templetFilePath == null)
throw new Exception("Excel模板文件路径不能为空!");
if (outputFilePath == null)
throw new Exception("输出Excel文件路径不能为空!");
if (!File.Exists(templetFilePath))
throw new Exception("指定路径的Excel模板文件不存在!");
this.templetFile = templetFilePath;
this.outputFile = outputFilePath;
//创建一个Application对象并使其可见
beforeTime = DateTime.Now;
app = new Excel.ApplicationClass();
app.Visible = true;
afterTime = DateTime.Now;
//打开模板文件,得到WorkBook对象
workBook = app.Workbooks.Open(templetFile, missing, missing, missing, missing, missing,
missing, missing, missing, missing, missing, missing, missing);
//得到WorkSheet对象
workSheet = (Excel.Worksheet)workBook.Sheets.get_Item(1);
}
开发者ID:woailuoli993,项目名称:graduate_old,代码行数:33,代码来源:ExcelHelper.cs
示例17: ReadStuInfoXLS
public void ReadStuInfoXLS(ref DataSet dsStuInfoXls,string getPath,int classNumbers,ref Hashtable className)
{
OleDbDataAdapter myAdp = null;
OleDbConnection oleConn = null;
string[] sheetName = new string[classNumbers];
DataTable[] stuInfoXlsTable = new DataTable[classNumbers];
try
{
m_objExcel = new Excel.Application();
m_objBooks = (Excel.Workbooks)m_objExcel.Workbooks;
m_objBook = (Excel._Workbook)(m_objBooks.Open( //打开该文件
getPath,Type.Missing,Type.Missing,Type.Missing,
Type.Missing,Type.Missing,Type.Missing,Type.Missing,
Type.Missing,Type.Missing,Type.Missing,Type.Missing,
Type.Missing));
m_objSheets = (Excel.Sheets)m_objBook.Worksheets;
for ( int i=0; i<classNumbers; i++ )
{
m_objSheet = (Excel._Worksheet)(m_objSheets.get_Item(i+1)); //记录工作表名称
sheetName[i] = m_objSheet.Name.Trim();
className.Add(i,sheetName[i]);
}
m_objBooks.Close();
oleConn = new OleDbConnection(@"provider=Microsoft.Jet.OLEDB.4.0;data source=" + getPath //打开驱动器
+ ";Extended Properties='Excel 8.0;HDR=Yes;IMEX=1'");
oleConn.Open();
for ( int i=0; i<classNumbers; i++ )
{
stuInfoXlsTable[i] = new DataTable("Table"+i.ToString());
myAdp = new OleDbDataAdapter("select * from ["+sheetName[i]+"$]", oleConn); //获取第一张工作表的内容
myAdp.Fill(stuInfoXlsTable[i]);
dsStuInfoXls.Tables.Add(stuInfoXlsTable[i]);
}
oleConn.Close();
System.Runtime.InteropServices.Marshal.ReleaseComObject(m_objSheet);
System.Runtime.InteropServices.Marshal.ReleaseComObject(m_objSheets);
System.Runtime.InteropServices.Marshal.ReleaseComObject(m_objBook);
System.Runtime.InteropServices.Marshal.ReleaseComObject(m_objBooks);
System.Runtime.InteropServices.Marshal.ReleaseComObject(m_objExcel);
}
catch(Exception e)
{
Util.WriteLog(e.Message,Util.EXCEPTION_LOG_TITLE);
}
finally
{
m_objSheet = null;
m_objSheets = null;
m_objBook = null;
m_objBooks = null;
m_objExcel = null;
GC.Collect();
KillProcess();
}
}
开发者ID:zesus19,项目名称:c4.v2.T,代码行数:63,代码来源:OptionRules.cs
示例18: ReadUpdateGradeInfo
public void ReadUpdateGradeInfo(ref DataTable dtUpdateGradeInfo,string getPath)
{
OleDbDataAdapter myAdp = null;
OleDbConnection oleConn = null;
try
{
m_objExcel = new Excel.Application();
m_objBooks = (Excel.Workbooks)m_objExcel.Workbooks;
m_objBook = (Excel._Workbook)(m_objBooks.Open( //打开该文件
getPath,Type.Missing,Type.Missing,Type.Missing,
Type.Missing,Type.Missing,Type.Missing,Type.Missing,
Type.Missing,Type.Missing,Type.Missing,Type.Missing,
Type.Missing));
m_objSheets = (Excel.Sheets)m_objBook.Worksheets;
oleConn = new OleDbConnection(@"provider=Microsoft.Jet.OLEDB.4.0;data source=" + getPath //打开驱动器
+ ";Extended Properties=Excel 8.0");
oleConn.Open();
myAdp = new OleDbDataAdapter("select * from [年班升级$]", oleConn); //获取第一张工作表的内容
myAdp.Fill(dtUpdateGradeInfo);
oleConn.Close();
System.Runtime.InteropServices.Marshal.ReleaseComObject(m_objSheet);
System.Runtime.InteropServices.Marshal.ReleaseComObject(m_objSheets);
System.Runtime.InteropServices.Marshal.ReleaseComObject(m_objBook);
System.Runtime.InteropServices.Marshal.ReleaseComObject(m_objBooks);
System.Runtime.InteropServices.Marshal.ReleaseComObject(m_objExcel);
}
catch(Exception e)
{
Util.WriteLog(e.Message,Util.EXCEPTION_LOG_TITLE);
}
finally
{
m_objSheet = null;
m_objSheets = null;
m_objBook = null;
m_objBooks = null;
m_objExcel = null;
GC.Collect();
KillProcess();
}
}
开发者ID:zesus19,项目名称:c4.v2.T,代码行数:47,代码来源:OptionRules.cs
示例19: WriteStuCardInfoXLS
public void WriteStuCardInfoXLS(DataSet dsStuInfo,string getPath)
{
KillProcess();
try
{
m_objExcel = new Excel.Application();
m_objBooks = (Excel.Workbooks)m_objExcel.Workbooks;
m_objBook = (Excel._Workbook)(m_objBooks.Open( //打开该文件
getPath,Type.Missing,Type.Missing,Type.Missing,
Type.Missing,Type.Missing,Type.Missing,Type.Missing,
Type.Missing,Type.Missing,Type.Missing,Type.Missing,
Type.Missing));
m_objSheets = (Excel.Sheets)m_objBook.Worksheets;
m_objSheet = (Excel._Worksheet)m_objSheets.get_Item(1);
object[,] objData = null;
if ( dsStuInfo.Tables[0].Rows.Count > 0 )
{
objData = new object[dsStuInfo.Tables[0].Rows.Count,2];
for ( int i=0; i<dsStuInfo.Tables[0].Rows.Count; i++ )
{
objData[i,0] = dsStuInfo.Tables[0].Rows[i][0].ToString();
objData[i,1] = dsStuInfo.Tables[0].Rows[i][1].ToString();
}
}
m_objRange = m_objSheet.get_Range("A2",m_objOpt);
m_objRange = m_objRange.get_Resize(dsStuInfo.Tables[0].Rows.Count,2);
m_objRange.Value = objData;
m_objRange.VerticalAlignment = Excel.XlVAlign.xlVAlignCenter;
m_objRange.HorizontalAlignment = Excel.XlHAlign.xlHAlignCenter;
m_objRange.Font.Size = 10;
m_objExcel.Visible = true;
}
catch(Exception e)
{
Util.WriteLog(e.Message,Util.EXCEPTION_LOG_TITLE);
}
finally
{
m_objSheet = null;
m_objSheets = null;
m_objBook = null;
m_objBooks = null;
m_objExcel = null;
GC.Collect();
}
}
开发者ID:zesus19,项目名称:c4.v2.T,代码行数:57,代码来源:OptionRules.cs
示例20: Generate
/// <summary>
/// Erzeugt die Listen mit den Objekten aus der Anmeldung.
/// </summary>
public void Generate()
{
// Variablen initialisieren
judoka = new List<Judoka>();
verein = new Verein();
// Ko-Kriterien
if (!System.IO.File.Exists(fileName)) return;
Excel.Application excel = null;
Excel.Workbook workbook = null;
Excel.Worksheet worksheet = null;
try
{
// Worksheet Initialisieren
excel = new Excel.Application();
workbook = excel.Workbooks.Open(fileName, 0, true, 5, "", "", true, Excel.XlPlatform.xlWindows, "\t", false, false, null, false, false, false);
worksheet = (Excel.Worksheet)workbook.Sheets.get_Item(1);
// Verein auslesen
verein.Name = GetSheetValue(worksheet, "B2");
verein.Kurzname = GetSheetValue(worksheet, "B2");
verein.Email = GetSheetValue(worksheet, "B7");
System.Text.StringBuilder sb = new StringBuilder();
sb.AppendLine(GetSheetValue(worksheet, "B3"));
sb.AppendLine(GetSheetValue(worksheet, "B4"));
sb.AppendLine(GetSheetValue(worksheet, "B5"));
sb.AppendLine(GetSheetValue(worksheet, "B6"));
verein.Adresse = sb.ToString();
// Kämpfer auslesen
int counter = 1;
while (HasSheetJudoka(counter, worksheet))
{
Judoka j = new Judoka();
j.Vorname = GetSheetValue(worksheet, GetJudokaCell(counter, "C"));
j.Nachname = GetSheetValue(worksheet, GetJudokaCell(counter, "B"));
try
{
j.Geburtsdatum = DateTime.FromOADate(double.Parse(GetSheetValue(worksheet, GetJudokaCell(counter, "F"))));
}
catch (Exception)
{
j.Geburtsdatum = new DateTime(1900, 1, 1);
}
j.Gürtel = new Gürtel(GetSheetValue(worksheet, GetJudokaCell(counter, "D")), "9");
j.Geschlecht = new Geschlecht(GetSheetValue(worksheet, GetJudokaCell(counter, "E")));
j.Verein = verein;
judoka.Add(j);
counter++;
}
}
catch (Exception)
{
throw;
}
finally
{
// Excel beenden
workbook.Close(false, null, 0);
workbook = null;
excel.Quit();
excel = null;
System.GC.WaitForPendingFinalizers();
}
}
开发者ID:BackupTheBerlios,项目名称:siesaso-svn,代码行数:72,代码来源:AnmeldungV4.cs
注:本文中的Excel.Application类示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论