Use EPPLUS to Operate EXcel and epplus to Operate excel
1 download Epplus latest: http://epplus.codeplex.com/
2. Add and reference EEPULS. dll to the project (flagship of the testing environment Win7 + V2010)
3 add using reference
Using OfficeOpenXml;
Using OfficeOpenXml. Drawing;
Using OfficeOpenXml. Drawing. Chart;
Using OfficeOpenXml. Style;
4 read Excel files
Private void button2_Click (object sender, EventArgs e) {string pfilePath = ""; OpenFileDialog ofd = new OpenFileDialog (); ofd. filter = "*. xls | *. xlsx "; if (ofd. showDialog () = System. windows. forms. dialogResult. OK) {pfilePath = ofd. fileName;} if (string. isNullOrEmpty (pfilePath) {MessageBox. show ("No selected file"); return;} FileInfo existingFile = new FileInfo (pfilePath); try {ExcelPackage package = new ExcelPackage (existingFile); int vSheetCount = package. workbook. worksheets. count; // get the total Sheet page ExcelWorksheet worksheet = package. workbook. worksheets [1]; // select the specified page int maxColumnNum = worksheet. dimension. end. column; // maximum Column int minColumnNum = worksheet. dimension. start. column; // minimum Column int maxRowNum = worksheet. dimension. end. row; // the smallest Row int minRowNum = worksheet. dimension. start. row; // The largest able vTable = new DataTable (); DataColumn vC; for (int j = 1; j <= maxColumnNum; j ++) {vC = new DataColumn ("A _" + j, typeof (string); vTable. columns. add (vC) ;}if (maxRowNum> 200) {maxRowNum = 200 ;}for (int n = 1; n <= maxRowNum; n ++) {DataRow vRow = vTable. newRow (); for (int m = 1; m <= maxColumnNum; m ++) {vRow [m-1] = worksheet. cells [n, m]. value;} vTable. rows. add (vRow);} this. dataGridView1.DataSource = vTable;} catch (Exception vErr) {MessageBox. show (vErr. message );}}
5. write an Excel file
Private void button#click (object sender, EventArgs e) {OfficeOpenXml. excelPackage ep = new OfficeOpenXml. excelPackage (); OfficeOpenXml. excelWorkbook wb = ep. workbook; OfficeOpenXml. excelWorksheet ws = wb. worksheets. add ("my worksheet"); // configuration file property wb. properties. category = "Category"; wb. properties. author = "Author"; wb. properties. comments = "Remarks"; wb. properties. company = "Company"; wb. properties. keywords = "keyword"; wb. properties. manager = "Manager"; wb. properties. status = "content Status"; wb. properties. subject = "topic"; wb. properties. title = "Title"; wb. properties. lastModifiedBy = "Last guaranteed"; // write data ws. cells [1, 1]. value = "Hello"; ws. column (1 ). width = 40; // modify the column Width ws. cells ["B1"]. value = "World"; ws. cells [3, 3, 3, 5]. merge = true; ws. cells [3, 3]. value = "Cells [3, 3, 3, 5] merge"; ws. cells ["A4: D5"]. merge = true; ws. cells ["A4"]. value = "Cells [\" A4: D5 \ "] merge"; SaveFileDialog sfd = new SaveFileDialog (); sfd. filter = "*. xls | *. xlsx "; if (sfd. showDialog () = System. windows. forms. dialogResult. OK) {FileInfo file = new FileInfo (sfd. fileName); ep. file = file; ep. save (); MessageBox. show ("saved successfully");} else {MessageBox. show ("unsave ");}}
For most of the above Code source networks, only small changes are made. The original text and data address are as follows:
Http://blog.csdn.net/jimmy2248/article/details/8363697
Http://blog.csdn.net/zhoufoxcn/article/details/14112473
Http://www.cnblogs.com/liudeyun/p/3535740.html
Http://blog.163.com/china__xuhua/blog/static/19972316920136335721285/
Use Epplus to Operate Excel, but does it support xls format? I have tried xlsx.
Program compatibility should be compatible with earlier versions and later versions, although I don't know what you said
Epplus
But I think it supports ~
When I recently used EPPlus 313 to operate an EXCEL file, an Error saving file E: \ *** was prompted. Which of the following experts gave me some advice,
Check whether the file is uploaded and the path of the saved file is correct.
This prompt does not have any practical significance. The prompt says that an "Export" command has an error when acting on your graph. The error is not prompted, so this problem cannot be solved, first, check your CDR software to see if only this file cannot be exported normally or all CDR files cannot be exported normally. If it is only this file, you can group all objects, press P to center all objects on the canvas, and then export and try. If not, and your CDR file can be opened normally, it is recommended that you save the file and check whether the file can be exported normally. If neither of these methods can be solved, it turns out that your CDR file is really damaged!
Hope to help you ~