程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> 關於.NET >> .net操作EXCEL詳解

.net操作EXCEL詳解

編輯:關於.NET

背景:

在項目中,需要操作EXCEL 文檔,本以為是OBA的應用,但其實不然。OBA是在EXCEL中嵌入.net應用插 件,而我們則是需要在SCSF中操作EXCEL。

我大致調查了一下,主要發現3種方式。

1:使用Microsoft.Office.Interop.Excel,調用EXCEL COM組件,操作EXCEL文件

2:使用OleDb 操作EXCEL數據源,進而利用ADO.net。

3:使用OPEN XML,訪問EXCEL zip文件並使用DOM。

實現:

1:使用.NET 調用COM

////////////////////////
private static Microsoft.Office.Interop.Excel.Application xApp;
..
if (xApp == null)
xApp = new Microsoft.Office.Interop.Excel.ApplicationClass();
Microsoft.Office.Interop.Excel.Workbook xBook = null;
xApp.Visible = false;
try
{
xBook = xApp.Workbooks._Open(@"c:\待發工資.xlsx", Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);
Microsoft.Office.Interop.Excel.Worksheet xSheet = (Microsoft.Office.Interop.Excel.Worksheet)xBook.Sheets[1];
//Excel.Worksheet xSheet=(Excel.Worksheet)xApp.ActiveSheet;
//Microsoft.Office.Interop.Excel.Range rng1 = xSheet.get_Range("A1", Type.Missing);
Microsoft.Office.Interop.Excel.Range cell = (Microsoft.Office.Interop.Excel.Range) xSheet.Cells[2, 1];
string str = "";
for (int i = 2; cell.Value2 != null; i++)
{
str += cell.Value2.ToString() + ".";
cell = (Microsoft.Office.Interop.Excel.Range)xSheet.Cells[2, i];
}
MessageBox.Show(str);
xBook.Close(Microsoft.Office.Interop.Excel.XlSaveAction.xlDoNotSaveChanges, @"c:\待發工 資.xlsx", Missing.Value);
xApp.Quit();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message.ToString());
}
///////////////////////

2: 使用oleDB(VSTS2008+Excel 2007)

//////////////////////
OleDbConnection objConn = null;
DataSet data = new DataSet();
try
{
string strConn = @"Provider=Microsoft.ACE.OleDb.12.0;Data Source=c:\待發工 資.xlsx;Extended Properties='Excel 12.0;HDR=YES'";//IMEX=1為只讀
//"Provider=Microsoft.Jet.OleDb.4.0;data source=c:\待發工資.xlsx;Extended Properties=""Excel 12.0;HDR=YES;IMEX=1""";
objConn = new OleDbConnection(strConn);
objConn.Open();
//System .Data . DataTable schemaTable = objConn.GetOleDbSchemaTable (System.Data.OleDb.OleDbSchemaGuid.Tables, null);
//int SheetIndex = 0;
//string tableName = schemaTable.Rows[SheetIndex][2].ToString().Trim();
string strSql = "Select * From [Sheet1$]";
OleDbCommand objCmd = new OleDbCommand(strSql, objConn);
OleDbDataAdapter sqlada = new OleDbDataAdapter();
sqlada.SelectCommand = objCmd;
sqlada.Fill(data);
string str = "";
for (int i = 0; i < data.Tables[0].Rows[0].ItemArray.Length; i++)
{
str += data.Tables[0].Rows[0].ItemArray[i].ToString() + ".";
}
MessageBox.Show(str);
objConn.Close();
}
catch (Exception ex)
{
objConn.Close();
Console.WriteLine(ex.Message.ToString());
}
//////////////////////

3:使用OPENXML (比較復雜的說)

//////////////////////
private const string documentRelationshipType =
"http://schemas.openxmlformats.org/officeDocument/" +
"2006/relationships/officeDocument";
private const string worksheetSchema =
"http://schemas.openxmlformats.org/spreadsheetml/2006/main";
private const string sharedStringsRelationshipType =
"http://schemas.openxmlformats.org/officeDocument/" +
"2006/relationships/sharedStrings";
private const string sharedStringSchema =
"http://schemas.openxmlformats.org/spreadsheetml/2006/main";
public static Package xlPackage;
private static Package OpenReadExcel(string fileName)
{
if (xlPackage == null)
xlPackage = Package.Open(fileName, FileMode.Open, FileAccess.Read);
return xlPackage;
}
private static Package OpenWriteExcel(string fileName)
{
if (xlPackage == null)
xlPackage = Package.Open(fileName, FileMode.Open, FileAccess.ReadWrite);
return xlPackage;
}
//////////////////////
public static string XLGetCellValue(string fileName, string sheetName, string addressName)
{
// Return the value of the specified cell.
string cellValue = null;
// Retrieve the stream containing the requested
// worksheet's info:
using (xlPackage = OpenReadExcel(fileName))
{
PackagePart documentPart = null;
Uri documentUri = null;
// Get the main document part (workbook.xml).
foreach (System.IO.Packaging.PackageRelationship relationship in
xlPackage.GetRelationshipsByType(documentRelationshipType))
{
// There should only be one document part in the package.
documentUri = PackUriHelper.ResolvePartUri(
new Uri("/", UriKind.Relative), relationship.TargetUri);
documentPart = xlPackage.GetPart(documentUri);
// There should only be one instance,
// but get out no matter what.
break;
}
if (documentPart != null)
{
// Load the contents of the workbook.
XmlDocument doc = new XmlDocument();
doc.Load(documentPart.GetStream());
// Create a namespace manager, so you can search.
// Add a prefix (d) for the default namespace.
NameTable nt = new NameTable();
XmlNamespaceManager nsManager = new XmlNamespaceManager(nt);
nsManager.AddNamespace("d", worksheetSchema);
nsManager.AddNamespace("s", sharedStringSchema);
string searchString = string.Format("//d:sheet[@name='{0}']", sheetName);
XmlNode sheetNode = doc.SelectSingleNode(searchString, nsManager);
if (sheetNode != null)
{
// Get the relId attribute:
XmlAttribute relationAttribute =
sheetNode.Attributes["r:id"];
if (relationAttribute != null)
{
string relId = relationAttribute.Value;
// First, get the relation between the
// document and the sheet.
PackageRelationship sheetRelation =
documentPart.GetRelationship(relId);
Uri sheetUri = PackUriHelper.ResolvePartUri(
documentUri, sheetRelation.TargetUri);
PackagePart sheetPart = xlPackage.GetPart(sheetUri);
// Load the contents of the workbook.
XmlDocument sheetDoc = new XmlDocument(nt);
sheetDoc.Load(sheetPart.GetStream());
// Next code block goes here.
XmlNode cellNode = sheetDoc.SelectSingleNode(string.Format("//d:sheetData/d:row/d:c [@r='{0}']", addressName), nsManager);
if (cellNode != null)
{
// Retrieve the value. The value may be stored within
// this element. If the "t" attribute contains "s", then
// the cell contains a shared string, and you must look
// up the value individually.
XmlAttribute typeAttr = cellNode.Attributes["t"];
string cellType = string.Empty;
if (typeAttr != null)
{
cellType = typeAttr.Value;
}
XmlNode valueNode = cellNode.SelectSingleNode("d:v", nsManager);
if (valueNode != null)
{
cellValue = valueNode.InnerText;
}
// Check the cell type. At this point, this code only checks
// for booleans and strings individually.
if (cellType == "b")
{
if (cellValue == "1")
{
cellValue = "TRUE";
}
else
{
cellValue = "FALSE";
}
}
else if (cellType == "s")
{
// Go retrieve the actual string from the associated string file.
foreach (System.IO.Packaging.PackageRelationship
stringRelationship in
documentPart.GetRelationshipsByType(sharedStringsRelationshipType))
{
// There should only be one shared string reference,
// so you exit this loop immediately.
Uri sharedStringsUri = PackUriHelper.ResolvePartUri(
documentUri, stringRelationship.TargetUri);
PackagePart stringPart = xlPackage.GetPart(sharedStringsUri);
if (stringPart != null)
{
// Load the contents of the shared strings.
XmlDocument stringDoc = new XmlDocument(nt);
stringDoc.Load(stringPart.GetStream());
// Add the string schema to the namespace manager:
nsManager.AddNamespace("s", sharedStringSchema);
int requestedString = Convert.ToInt32(cellValue);
string strSearch = string.Format(
"//s:sst/s:si[{0}]", requestedString + 1);
XmlNode stringNode =
stringDoc.SelectSingleNode(strSearch, nsManager);
if (stringNode != null)
{
cellValue = stringNode.InnerText;
}
}
}
}
}
}
}
return cellValue;
}
}
return "";
}
/////////////////////

public static bool XLInsertNumberIntoCell(string fileName, string sheetName, string addressName, string value)
{
// Retrieve the stream containing the requested
// worksheet's info:
PackagePart documentPart = null;
Uri documentUri = null;
bool returnValue = false;
XmlDocument xDoc = null;
XmlDocument doc = null;
PackagePart sheetPart = null;
using (xlPackage = OpenWriteExcel(fileName))
{
// Get the main document part (workbook.xml).
foreach (System.IO.Packaging.PackageRelationship relationship
in xlPackage.GetRelationshipsByType(documentRelationshipType))
{
// There should only be one document part in the package.
documentUri = PackUriHelper.ResolvePartUri(new Uri("/",
UriKind.Relative), relationship.TargetUri);
documentPart = xlPackage.GetPart(documentUri);
// There should only be one instance,
// but get out no matter what.
break;
}
// Code removed here.
if (documentPart != null)
{
// Load the contents of the workbook.
doc = new XmlDocument();
doc.Load(documentPart.GetStream());
// Create a NamespaceManager to handle the default namespace,
// and create a prefix for the default namespace:
XmlNamespaceManager nsManager =
new XmlNamespaceManager(doc.NameTable);
nsManager.AddNamespace("d", doc.DocumentElement.NamespaceURI);
// Code removed here
string searchString =
string.Format("//d:sheet[@name='{0}']", sheetName);
XmlNode sheetNode = doc.SelectSingleNode(searchString, nsManager);
if (sheetNode != null)
{
// Get the relId attribute:
XmlAttribute relationAttribute = sheetNode.Attributes["r:id"];
if (relationAttribute != null)
{
string relId = relationAttribute.Value;
// First, get the relation between the document and the sheet.
PackageRelationship sheetRelation =
documentPart.GetRelationship(relId);
Uri sheetUri = PackUriHelper.
ResolvePartUri(documentUri, sheetRelation.TargetUri);
sheetPart = xlPackage.GetPart(sheetUri);
// Load the contents of the sheet into an XML document.
xDoc = new XmlDocument();
xDoc.Load(sheetPart.GetStream());
// Code removed here.
// Use regular expressions to get the row number.
// If the parameter wasn't well formed, this code
// will fail:
System.Text.RegularExpressions.Regex r =
new System.Text.RegularExpressions.Regex(@"^(?<col>\D+)(?<row>\d+) ");
string rowNumber = r.Match(addressName).Result("${row}");
// Search for the existing cell:
XmlNode cellnode = xDoc.SelectSingleNode(
string.Format("//d:sheetData/d:row/d:c[@r='{0}']", addressName),
nsManager);
if (cellnode == null)
{
// Code removed here.
XmlElement cellElement = xDoc.CreateElement("c", worksheetSchema);
cellElement.Attributes.Append(xDoc.CreateAttribute("r"));
cellElement.Attributes["r"].Value = addressName;
XmlElement valueElement = xDoc.CreateElement("v", worksheetSchema);
valueElement.InnerText = value;
cellElement.AppendChild(valueElement);
// Default style is "0"
cellElement.Attributes.Append(xDoc.CreateAttribute("s"));
cellElement.Attributes["s"].Value = "0";
XmlNode rowNode = xDoc.SelectSingleNode(string.Format(
"//d:sheetData/d:row[@r='{0}']", rowNumber), nsManager);
if (rowNode == null)
{
// Code removed here…
// Didn't find the row, either. Just add a new row element:
XmlNode sheetDataNode = xDoc.SelectSingleNode("//d:sheetData", nsManager);
if (sheetDataNode != null)
{
XmlElement rowElement = xDoc.CreateElement("row", worksheetSchema);
rowElement.Attributes.Append(xDoc.CreateAttribute("r"));
rowElement.Attributes["r"].Value = rowNumber;
rowElement.AppendChild(cellElement);
sheetDataNode.AppendChild(rowElement);
returnValue = true;
}
}
else
{
// Code removed here…
XmlAttribute styleAttr =
((XmlAttribute)(rowNode.Attributes.GetNamedItem("s")));
if (styleAttr != null)
{
// You know cellElement has an "s" attribute -- you
// added it yourself.
cellElement.Attributes["s"].Value = styleAttr.Value;
}
// You must insert the new cell at the correct location.
// Loop through the children, looking for the first cell that is
// beyond the cell you're trying to insert. Insert before that cell.
XmlNode biggerNode = null;
XmlNodeList cellNodes = rowNode.SelectNodes("./d:c", nsManager);
if (cellNodes != null)
{
foreach (XmlNode node in cellNodes)
{
if (String.Compare(node.Attributes["r"].Value, addressName) > 0)
{
biggerNode = node;
break;
}
}
}
if (biggerNode == null)
{
rowNode.AppendChild(cellElement);
}
else
{
rowNode.InsertBefore(cellElement, biggerNode);
}
returnValue = true;
}
}
else
{
// Code removed here.
cellnode.Attributes.RemoveNamedItem("t");
XmlNode valueNode = cellnode.SelectSingleNode("d:v", nsManager);
if (valueNode == null)
{
// Cell with deleted value. Add a value element now.
valueNode = xDoc.CreateElement("v", worksheetSchema);
cellnode.AppendChild(valueNode);
}
valueNode.InnerText = value.ToString();
returnValue = true;
}
}
}
// Save the XML back to its part.
xDoc.Save(sheetPart.GetStream(FileMode.Create, FileAccess.Write));
}
}
return returnValue;
}
/////////////////////

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved