打开文件的不同方式
如何通过路径打开Excel文件
开发人员可以通过在构造函数中指定本地计算机上的文件路径来打开Microsoft Excel文件。只需将路径作为string传递给构造函数。Aspose.Cells将自动检测文件格式类型。
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); | |
// Opening through Path | |
// Creating a Workbook object and opening an Excel file using its file path | |
Workbook workbook1 = new Workbook(dataDir + "Book1.xlsx"); | |
Console.WriteLine("Workbook opened using path successfully!"); |
如何通过流打开Excel文件
通过流打开Excel文件也很简单。为此,请使用接收包含文件的流对象的构造函数的重载版本。
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); | |
// Opening through Stream | |
// Create a Stream object | |
FileStream fstream = new FileStream(dataDir + "Book2.xls", FileMode.Open); | |
// Creating a Workbook object, open the file from a Stream object | |
// That contains the content of file and it should support seeking | |
Workbook workbook2 = new Workbook(fstream); | |
Console.WriteLine("Workbook opened using stream successfully!"); | |
fstream.Close(); |
如何只打开具有数据的文件
要仅打开具有数据的文件,使用LoadOptions和LoadFilter类设置文件模板的相关属性和选项。
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); | |
// Load only specific sheets with data and formulas | |
// Other objects, items etc. would be discarded | |
// Instantiate LoadOptions specified by the LoadFormat | |
LoadOptions loadOptions = new LoadOptions(LoadFormat.Xlsx); | |
// Set LoadFilter property to load only data & cell formatting | |
loadOptions.LoadFilter = new LoadFilter(LoadDataFilterOptions.CellData); | |
// Create a Workbook object and opening the file from its path | |
Workbook book = new Workbook(dataDir + "Book1.xlsx", loadOptions); | |
Console.WriteLine("File data imported successfully!"); |
如何仅加载可见工作表
在加载 Workbook 时,有时您可能只需要工作簿中可见工作表中的数据。Aspose.Cells允许您在加载工作簿时跳过不可见工作表中的数据。要做到这一点,请创建一个继承 LoadFilter 类的自定义函数,并将其实例传递给 LoadOptions.LoadFilter 属性。
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); | |
string sampleFile = "output.xlsx"; | |
string samplePath = dataDir + sampleFile; | |
// Create a sample workbook | |
// and put some data in first cell of all 3 sheets | |
Workbook createWorkbook = new Workbook(); | |
createWorkbook.Worksheets["Sheet1"].Cells["A1"].Value = "Aspose"; | |
createWorkbook.Worksheets.Add("Sheet2").Cells["A1"].Value = "Aspose"; | |
createWorkbook.Worksheets.Add("Sheet3").Cells["A1"].Value = "Aspose"; | |
createWorkbook.Worksheets["Sheet3"].IsVisible = false; | |
createWorkbook.Save(samplePath); | |
// Load the sample workbook | |
LoadOptions loadOptions = new LoadOptions(); | |
loadOptions.LoadFilter = new CustomLoad(); | |
Workbook loadWorkbook = new Workbook(samplePath, loadOptions); | |
Console.WriteLine("Sheet1: A1: {0}", loadWorkbook.Worksheets["Sheet1"].Cells["A1"].Value); | |
Console.WriteLine("Sheet1: A2: {0}", loadWorkbook.Worksheets["Sheet2"].Cells["A1"].Value); | |
Console.WriteLine("Sheet1: A3: {0}", loadWorkbook.Worksheets["Sheet3"].Cells["A1"].Value); |
以下是上述片段中引用的CustomnLoad类的实现。
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
class CustomLoad : LoadFilter | |
{ | |
public override void StartSheet(Worksheet sheet) | |
{ | |
if (sheet.IsVisible) | |
{ | |
// Load everything from visible worksheet | |
this.LoadDataFilterOptions = LoadDataFilterOptions.All; | |
} | |
else | |
{ | |
// Load nothing | |
this.LoadDataFilterOptions = LoadDataFilterOptions.Structure; | |
} | |
} | |
} |