从读取文件导出多行到Excel
我有一些代码正在用于从Excel电子表格中获取一行数据,并将其转换为另一个电子表格发送给我的客户。 我想要做的是让这个代码在最终的电子表格上返回多行。 现在我正在这样做:
using (OleDbConnection conn = new OleDbConnection(pathConn)) { for (int i = 0; i < excelSheets.Length; i++) { OleDbDataAdapter adapter = new OleDbDataAdapter("Select * from [" + excelSheets[i] + "]", conn); System.Data.DataTable dt = new System.Data.DataTable(); adapter.Fill(dt); tring dtBrokerName = dt.Rows[0].Field<string>("ProducerName"); //[Sets numerous fields] // create final output report based on imported data GenerateExcelData(dtBrokerName, dtExpirationDate, dtPolicyNumber, dtCurrentPrice, tier, usage, city, dtcValue); } }
我将最终报告中所需的信息传递给此方法:
public void GenerateExcelData(string dtBrokerName, DateTime dtExpirationDate, string dtPolicyNumber, double dtCurrentPrice, string tier, string usage, string city, decimal dtcValue) { CreateExcelDoc BatchRenewalReport = new CreateExcelDoc(); //creates the header row BatchRenewalReport.createHeaders(1, 1, "Producer", "A1", "A1", 0, "#", true, 30, "n"); BatchRenewalReport.createHeaders(1, 2, "Expiration Date", "B1", "B1", 0, "#", true, 15, "n"); BatchRenewalReport.createHeaders(1, 3, "Policy #", "C1", "C1", 0, "#", true, 12.86, "n"); BatchRenewalReport.createHeaders(1, 4, "Current Price", "D1", "D1", 0, "#", true, 15.71, "n"); BatchRenewalReport.createHeaders(1, 5, "New Price", "E1", "E1", 0, "#", true, 15.71, "n"); BatchRenewalReport.createHeaders(1, 6, "$ Difference", "F1", "F1", 0, "#", true, 12.14, "n"); BatchRenewalReport.createHeaders(1, 7, "% Difference", "G1", "G1", 0, "#", true, 12.14, "n"); BatchRenewalReport.createHeaders(1, 8, "Tier", "H1", "H1", 0, "#", true, 15, "n"); BatchRenewalReport.createHeaders(1, 9, "Usage", "I1", "I1", 0, "#", true, 9, "n"); BatchRenewalReport.createHeaders(1, 10, "City", "J1", "J1", 0, "#", true, 16.43, "n"); BatchRenewalReport.createHeaders(1, 11, "DTC", "K1", "K1", 0, "#", true, 6.43, "n"); //fills additional rows BatchRenewalReport.addData(2, 1, dtBrokerName, "A2", "A2", ""); BatchRenewalReport.addData(2, 2, dtExpirationDate.ToString(), "B2", "B2", "mm/dd/yyyy"); BatchRenewalReport.addData(2, 3, dtPolicyNumber, "C2", "C2", ""); BatchRenewalReport.addData(2, 4, dtCurrentPrice.ToString(), "D2", "D2", "$#,##0"); BatchRenewalReport.addData(2, 5, "3117", "E2", "E2", "$#,##0"); BatchRenewalReport.addData(2, 6, "=E2-D2", "F2", "F2", "$#,##0"); BatchRenewalReport.addData(2, 7, "=(E2/D2)-1", "G2", "G2", "#.#0%"); BatchRenewalReport.addData(2, 8, tier, "H2", "H2", ""); BatchRenewalReport.addData(2, 9, usage, "I2", "I2", ""); BatchRenewalReport.addData(2, 10, city, "J2", "J2", ""); BatchRenewalReport.addData(2, 11, dtcValue.ToString(), "K2", "K2", "0#.##0"); }
它利用这个由第三方开发的类:
class CreateExcelDoc { private Excel.Application app = null; private Excel.Workbook workbook = null; private Excel.Worksheet worksheet = null; private Excel.Range workSheet_range = null; public CreateExcelDoc() { createDoc(); } public void createDoc() { try { app = new Excel.Application(); app.Visible = true; workbook = app.Workbooks.Add(1); worksheet = (Excel.Worksheet)workbook.Sheets[1]; } catch (Exception e) { Console.Write("Error"); } finally { } } public void createHeaders(int row, int col, string htext, string cell1, string cell2, int mergeColumns, string b, bool font, double size, string fcolor) { worksheet.Cells[row, col] = htext; workSheet_range = worksheet.get_Range(cell1, cell2); workSheet_range.Merge(mergeColumns); switch (b) { case "Black": workSheet_range.Interior.Color = Color.Black.ToArgb(); break; case "Gray": workSheet_range.Interior.Color = Color.Gray.ToArgb(); break; case "Red": workSheet_range.Interior.Color = Color.Red.ToArgb(); break; case "Green": workSheet_range.Interior.Color = Color.ForestGreen.ToArgb(); break; case "Yellow": workSheet_range.Interior.Color = Color.LightYellow.ToArgb(); break; default: // workSheet_range.Interior.Color = System.Drawing.Color..ToArgb(); break; } workSheet_range.Borders.Color = Color.Black.ToArgb(); workSheet_range.Font.Bold = font; workSheet_range.ColumnWidth = size; if (fcolor.Equals("")) { workSheet_range.Font.Color = Color.White.ToArgb(); } else { workSheet_range.Font.Color = Color.Black.ToArgb(); } } public void addData(int row, int col, string data, string cell1, string cell2, string format) { worksheet.Cells[row, col] = data; workSheet_range = worksheet.get_Range(cell1, cell2); workSheet_range.Borders.Color = Color.Black.ToArgb(); workSheet_range.NumberFormat = format; } }
我试图让程序读取我导入的Excel文件中的每一行,并在导出报告中逐行返回值。 我试图改变:
BatchRenewalReport.addData(2, 1, dtBrokerName, "A2", "A2", "");
目前为止还没有成功。