博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
c# 读写excel文件
阅读量:4042 次
发布时间:2019-05-24

本文共 3956 字,大约阅读时间需要 13 分钟。

**首先项目要引用excel.dll:Microsoft.Office.Interop.Excel

/// 
public string[] Excel_Read(string filePath, string[] cellAddress,string workSheetName) {
if (!File.Exists(fileFullName)) {
return null; } List
listExcelVal = new List
(); Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application(); Microsoft.Office.Interop.Excel.Workbooks wbooks = xlApp.Workbooks; // string fileCopyTemp = FileCopy(filePath); Microsoft.Office.Interop.Excel.Workbook wbook = wbooks.Open(filePath, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing); Microsoft.Office.Interop.Excel.Worksheet worksheet = wbook.Worksheets[workSheetName]; //"Inf" sheetname try {
// Write the data //for (int j = 0; j <= cellAddress.Length - 1; j++) // {
// worksheet.Range(cellAddress(i).ToString()).Value2 = "test test test"; // } // read the data ,support address range ,eg: [B8:B49] for (int j = 0; j <= cellAddress.Length - 1; j++) {
switch (cellAddress[j].ToString()) {
case "": listExcelVal.Add(""); break; default: object obj; Microsoft .Office .Interop.Excel .Range range = worksheet.Range[cellAddress[j].ToString()]; foreach (Microsoft.Office.Interop.Excel.Range cell in range) {
obj = cell.Text; listExcelVal.Add(obj.ToString()); } //obj = worksheet.Range[cellAddress[j].ToString()].Value2; // obj is a Object[,] value //foreach (var item in (object[,])obj) //{
// // foreach (var item in (object[])items) // // {
// listExcelVal.Add(Convert.ToString(item)); // listExcelVal.Add(item.GetType().Name.ToUpper() == "DOUBLE" ? (Convert.ToDouble(Convert.ToString(item)).ToString("f2")) : Convert.ToString(item)); // //} //} break; } } } catch (Exception e) {
// throw e; listExcelVal.Clear(); listExcelVal.Add("Error in getting excel data."); } finally {
wbook.Close(false, filePath, false); //不會彈出"是否保存"框 xlApp.Quit(); ReleaseObj(xlApp); ReleaseObj(wbook); ReleaseObj(worksheet); } return listExcelVal.ToArray(); } //释放资源 private void ReleaseObj(Object o) {
try {
//使用此方法,来释放引用某些资源的基础 COM 对象。 这里的o就是要释放的对象 System.Runtime.InteropServices.Marshal.ReleaseComObject(o); } catch {
} finally {
o = null; GC.Collect(); } }

转载地址:http://ypmdi.baihongyu.com/

你可能感兴趣的文章
解决python2.7中UnicodeEncodeError
查看>>
小谈python 输出
查看>>
Django objects.all()、objects.get()与objects.filter()之间的区别介绍
查看>>
python:如何将excel文件转化成CSV格式
查看>>
机器学习实战之决策树(一)
查看>>
机器学习实战之决策树二
查看>>
[LeetCode By Python]7 Reverse Integer
查看>>
[leetCode By Python] 14. Longest Common Prefix
查看>>
[LeetCode By Python]118. Pascal's Triangle
查看>>
[LeetCode By Python]121. Best Time to Buy and Sell Stock
查看>>
[LeetCode By Python]122. Best Time to Buy and Sell Stock II
查看>>
[LeetCode By Python]125. Valid Palindrome
查看>>
[LeetCode By Python]136. Single Number
查看>>
[LeetCode By MYSQL] Combine Two Tables
查看>>
Android下调用收发短信邮件等(转载)
查看>>
Android中电池信息(Battery information)的取得
查看>>
SVN客户端命令详解
查看>>
Android/Linux 内存监视
查看>>
Linux系统信息查看
查看>>
用find命令查找最近修改过的文件
查看>>