本文介绍了c#中的Excel报告问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在winforms应用程序中生成报告,Mysql数据库,excel表格正确创建并且每件事都很好,但是当我关闭系统时它会问保存book1,book2 .....
我们生成报告的次数,它要求在系统关闭时保存。
使用以下代码:
am generating reports in winforms application,Mysql database ,excel sheet is created properly and every thing is fine, but when i shutdown the system it's asking to save book1,book2.....
as how many times we generate report, it is asking to save at system shutdown.
am using following code:
<pre lang="c#">using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
using System.Runtime.InteropServices;
using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.text.html;
using System.Diagnostics;
using iTextSharp.text.html.simpleparser;
using Microsoft.Office.Interop.Excel;
using System.Configuration;
using System.IO;
using System.Reflection;
namespace Billling_Machine
{
public partial class Reports : Form
{
String ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["myConnectionString"].ConnectionString;
MySqlDataAdapter da;
DataSet ds;
public Reports()
{
InitializeComponent();
}
private void generate_Click(object sender, EventArgs e)
{
int datasetvalue;
int datatablevalue;
try
{
using(MySqlConnection con=new MySqlConnection(ConnectionString))
{
String query;
StringBuilder queryBuilder = new StringBuilder();
MySqlCommand command = new MySqlCommand();
System.Data.DataTable dt = new System.Data.DataTable();
string fromDate = dateTimePicker1.Value.ToString("yyyy-MM-dd");
string toDate = dateTimePicker2.Value.ToString("yyyy-MM-dd");
if ((reportsmachine.Text == "") && (reportscard.Text == "") &&(reportscustomer.Text==""))
{
query = "select * from transaction where DATE(paid_date)BETWEEN '" + fromDate + "'AND '" + toDate + "' ";
da = new MySqlDataAdapter(query, con);
ds = new DataSet();
datasetvalue= da.Fill(ds);
datatablevalue=da.Fill(dt);
}
else
{
query = "select * from transaction where DATE(paid_date)BETWEEN '" + fromDate + "'AND '" + toDate + "' ";
if (reportsmachine.Text != "")
{
query = query + " AND " + "machine_id=" + "'" + reportsmachine.Text + "' ";
}
if (reportscustomer.Text != "")
{
query = query + " AND " + "customer_id=" + "'" + reportscustomer.Text + "' ";
}
if (reportscard.Text != "")
{
query = query + " AND " + "card_id=" + "'" + reportscard.Text + "' ";
}
da = new MySqlDataAdapter(query, con);
ds = new DataSet();
datasetvalue = da.Fill(ds);
datatablevalue = da.Fill(dt);
}
if (datasetvalue != 0 && datatablevalue != 0)
{
if (Convert.ToString(comboBox1.SelectedItem) == "EXCEL")
{
Microsoft.Office.Interop.Excel.Application excelApp = new Microsoft.Office.Interop.Excel.Application();
Microsoft.Office.Interop.Excel.Workbook workbook = (Microsoft.Office.Interop.Excel.Workbook)excelApp.Workbooks.Add(Missing.Value);
Microsoft.Office.Interop.Excel.Worksheet worksheet;
worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Worksheets[1];
worksheet.Cells[1, 1] = "Machine ID";
worksheet.Cells[1, 2] = "Customer ID";
worksheet.Cells[1, 3] = "Card ID";
worksheet.Cells[1, 4] = "Name";
worksheet.Cells[1, 5] = "Address";
worksheet.Cells[1, 6] = "Phone No";
worksheet.Cells[1, 7] = "Item1 Name";
worksheet.Cells[1, 8] = "Item1 Rate";
worksheet.Cells[1, 9] = "Item1 Quantity";
worksheet.Cells[1, 10] = "Item1 Total";
worksheet.Cells[1, 11] = "Item2 Name";
worksheet.Cells[1, 12] = "Item2 Rate";
worksheet.Cells[1, 13] = "Item2 Quantity";
worksheet.Cells[1, 14] = "Item2 Total";
worksheet.Cells[1, 15] = "Item3 Name";
worksheet.Cells[1, 16] = "Item3 Rate";
worksheet.Cells[1, 17] = "Item3 Quantity";
worksheet.Cells[1, 18] = "Item3 Total";
worksheet.Cells[1, 19] = "Handling Cost";
worksheet.Cells[1, 20] = "Billed Amount";
worksheet.Cells[1, 21] = "Received Amount";
worksheet.Cells[1, 22] = "Paid Date";
worksheet.Cells[1, 23] = "Due Amount";
int row = 1;
for (int k = 1; k <= 23; k++)
{
worksheet.Cells[row, k].Interior.ColorIndex = 39;
}
string data = null;
int i = 0;
int j = 0;
for (i = 0; i <= ds.Tables[0].Rows.Count-1; i++)
{
for (j = 0; j <= ds.Tables[0].Columns.Count - 1; j++)
{
data = ds.Tables[0].Rows[i].ItemArray[j].ToString();
((Microsoft.Office.Interop.Excel.Range)worksheet.Cells[i+2 , j + 1]).Value2 = data;
}
}
excelApp.Visible = true;
}//if}}
推荐答案
这篇关于c#中的Excel报告问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!